02.225 2022-05-25 17:22 采纳率: 37.5%
浏览 23
已结题

关于#android#的问题,如何解决?

问题遇到的现象和发生背景

在制作android实战项目开发时遇到了以下问题

问题相关代码,请勿粘贴截图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/weather">
    <TextView
        android:id="@+id/TV_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="北京"
        android:textSize="50sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"/>
    <TextView
        android:id="@+id/TV_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="10dp"
        android:text="用户名"
        android:textSize="30sp" />
    <ImageView
        android:id="@+id/IV_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="60dp"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:id="@+id/TV_weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/IV_icon"
        android:text="多云"
        android:textSize="30sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="60dp"/>
    <LinearLayout
        android:id="@+id/L_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/IV_icon"
        android:layout_marginLeft="45dp"
        android:layout_marginStart="45dp"
        android:layout_toEndOf="@id/IV_icon"
        android:layout_toRightOf="@id/IV_icon"
        android:gravity="center"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/TV_temp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="-7°"
            android:textSize="25sp"/>
        <TextView
            android:id="@+id/TV_wind"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="风力:三级"
            android:textSize="25sp"/>
        <TextView
            android:id="@+id/TV_pm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="pm"
            android:textSize="25sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:orientation="horizontal"
        android:layout_marginLeft="10dp"
        >
        <Button
            android:onClick="onClick"
            android:id="@+id/Beijing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="北京"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="20dp"/>
        <Button
            android:onClick="onClick"
            android:id="@+id/Shanghai"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="上海" />
        <Button
            android:onClick="onClick"
            android:id="@+id/Guangzhou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="广州" />
        <Button
            android:onClick="onClick"
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="存储" />
    </LinearLayout>
</RelativeLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity2 extends AppCompatActivity {

    MyDBOpenHelper dbOpenHelper = new MyDBOpenHelper(this);
    SQLiteDatabase db;
    JSONObject jsonObject;
    public static final int UPDATE = 0x1;

    //定义对象
    private ImageView IV_icon;
    private TextView TV_city,TV_user,TV_weather,TV_temp,TV_wind,TV_pm;
    private Button  Beijing,Shanghai,Guangzhou,save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //绑定控件
        IV_icon = (ImageView) findViewById(R.id.IV_icon);
        TV_city = (TextView) findViewById(R.id.TV_city);
        TV_user = (TextView) findViewById(R.id.TV_user);
        TV_weather = (TextView) findViewById(R.id.TV_weather);
        TV_temp = (TextView) findViewById(R.id.TV_temp);
        TV_wind = (TextView) findViewById(R.id.TV_wind);
        TV_pm = (TextView) findViewById(R.id.TV_pm);
        save = (Button) findViewById(R.id.save);

        //天气页面-用户名展示
        Intent intent = getIntent();
        String username = intent.getStringExtra("username");
        TV_user = (TextView) findViewById(R.id.TV_user);
        TV_user.setText("用户名" + username);
    }

    //按钮的监听
    public void onClick(View v){
        System.out.println(20220525);
        switch (v.getId()){
            case R.id.Beijing:
                network("北京");
                break;
            case R.id.Shanghai:
                network("上海");
                break;
            case R.id.Guangzhou:
                network("广州");
                break;
            case R.id.save:
                save();
                break;
        }
    }

    private Handler handler = new Handler(){
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATE:
                    String jsonData = (String) msg.obj;
                    try {
                        jsonObject = new JSONObject(jsonData);
                        String cityname = jsonObject.getString("city");
                        String weather = jsonObject.getString("weather");
                        String temp = jsonObject.getString("temperature");
                        String wind = jsonObject.getString("wind");
                        String pm = jsonObject.getString("air".trim());
                        TV_city.setText(cityname);
                        TV_weather.setText(weather);
                        TV_temp.setText(temp);
                        TV_wind.setText(wind);
                        TV_pm.setText(pm);
                        if(weather.equals("晴")){
                            IV_icon.setImageResource(R.drawable.sun);
                        }else  if  (weather.equals("阴")){
                            IV_icon.setImageResource(R.drawable.cloud_sun);
                        }else  if  (weather.equals("多云")){
                            IV_icon.setImageResource(R.drawable.clouds);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    public void network(final String CityName){
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = UrlUtils.getTemp_url(CityName);
                String jsonData = HttpUtils.getJsonContent(url);
                System.out.println("-----------------"+jsonData);
                Message msg = new Message();
                msg.what = UPDATE;
                msg.obj = jsonData;
                handler.sendMessage(msg);
            }
        }).start();
    }

    public void save() {
        String city;
        String pm;
        String weather;
        String temp;
        String wind;

        SQLiteDatabase db;
        ContentValues values;

        city=TV_city.getText().toString();
        pm=TV_pm.getText().toString();
        weather=TV_weather.getText().toString();
        temp=TV_temp.getText().toString();
        wind=TV_wind.getText().toString();

        db=dbOpenHelper.getWritableDatabase();

        values=new ContentValues();
        values.put("city",city);
        values.put("weather",weather);
        values.put("temperature",temp);
        values.put("air",pm);
        values.put("wind",wind);
        db.insert("weather_info",null,values);
        db.close();

        Toast.makeText(this,"成功",Toast.LENGTH_SHORT).show();
    }
}

package com.example.myapplication;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {
    public static String getJsonContent(String path){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream is = conn.getInputStream();
            int hasRead = 0;
            byte[]buf = new byte[1024];
            while ((hasRead = is.read(buf))!=-1){
                baos.write(buf,0,hasRead);
            }
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos.toString();
    }
}
package com.example.myapplication;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {
    public static String getJsonContent(String path){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream is = conn.getInputStream();
            int hasRead = 0;
            byte[]buf = new byte[1024];
            while ((hasRead = is.read(buf))!=-1){
                baos.write(buf,0,hasRead);
            }
            conn.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos.toString();
    }
}
运行结果及报错内容

img

我想要达到的结果

可以正常运行程序,可以正常切换城市,达到实时天气预报的一个效果

  • 写回答

1条回答 默认 最新

  • 立青_ 2022-05-25 17:31
    关注

    你访问的接口能连通吗?你有没有写网络权限?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月2日
  • 已采纳回答 5月25日
  • 修改了问题 5月25日
  • 创建了问题 5月25日

悬赏问题

  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题
  • ¥30 gradle环境下javafx项目如何使用druid连接池
  • ¥15 服务器打印水晶报表问题
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题