HttpUrlConnection+Json应用实例(天气预报)

HttpUrlConnection+ Json应用实例(天气预报)

一.效果图如下:

这里写图片描述

这里写图片描述

二.代码如下:
1.布局文件:

  <EditText
        android:id="@+id/main4_edit"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="请输入城市名"/>

    <Button
        android:id="@+id/main4_btn"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="查询"/>

    <TextView
        android:id="@+id/main4_text1"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <TextView
        android:id="@+id/main4_text2"
        android:layout_width="match_parent"
        android:layout_height="60dp" />
    <TextView
        android:id="@+id/main4_text3"
        android:layout_width="match_parent"
        android:layout_height="60dp" />

2.Java文件:

public class Main4Activity extends AppCompatActivity {

    private EditText cityET;
    private Button button;
    private TextView windTV;
    private TextView tempTV;
    private TextView weatherTV;
    private String API = "https://free-api.heweather.com/s6/weather-now?key=f4df0dc926f54557b9c94b6c09451b51&location=";

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

        bindID();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               String city =cityET.getText().toString();
                new MyTask().execute(API+city);

            }
        });
    }

    class MyTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... strings) {
            StringBuffer stringBuffer = new StringBuffer();


            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();//建立水闸
                InputStream inputStream = null;//建立输入流
                if (connection.getResponseCode() == 200)//判断网络是否正常 {
                    inputStream = connection.getInputStream();//只有网络正常切返回数据正常,我们才能创建输入流
                } else {
                    return "network_fail";
                }


        InputStreamReader reader = new InputStreamReader(inputStream);

         BufferedReader bufferedReader = new BufferedReader(reader);


                String temp = "";


            while ((temp = bufferedReader.readLine()) != null) {
                    stringBuffer.append(temp);
                }
                bufferedReader.close();
                reader.close();
                inputStream.close();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


            return stringBuffer.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s.equals("network_failed")) {

       Toast.makeText(Main4Activity.this, "网络失败", Toast.LENGTH_SHORT).show();
            } else {
                //解析Json
                try {
                    JSONObject object = new JSONObject(s);
                    JSONArray array = object.getJSONArray("HeWeather6");
                    JSONObject object1 = array.getJSONObject(0);
                    JSONObject nowObj = object1.getJSONObject("now");
                    String temp = nowObj.getString("tmp");
                    String wind = nowObj.getString("wind_dir") + nowObj.getString("wind_sc") + "级";
                    String weather = nowObj.getString("cond_txt");

                    weatherTV.setText(weather);
                    windTV.setText(wind);
                    tempTV.setText(temp);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

        private void bindID() {
        //绑定ID
            cityET = findViewById(R.id.main4_edit);
            button = findViewById(R.id.main4_btn);
            windTV = findViewById(R.id.main4_text1);
            tempTV = findViewById(R.id.main4_text2);
            weatherTV = findViewById(R.id.main4_text3);
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值