利用HttpUrlConnection和JSON来做简易的天气预报

首先谈一谈大体上思路,使用HttpUrlConnection发送Web请求去获取网页上的JSON数据,然后解析JSON数据来进行更新UI.

创建Activity和完成xml当中的控件。完成控件的绑定和监听:

public class weatherActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private TextView textView;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private EditText editText;
    private String num;
    private String Json_str;

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

    }

    private void BlindID() {
        button=findViewById(R.id.btn);
        textView=findViewById(R.id.text);
        textView2=findViewById(R.id.text1);
        textView3=findViewById(R.id.text2);
        textView4=findViewById(R.id.text3);
        editText=findViewById(R.id.edit);
        button.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn:
                num= "https://free-api.heweather.com/s6/weather/now?parameters&key=f13f32d6c9824100a9b3da90e9ed2944&&location="+editText.getText().toString();
                new weather().execute(num);

                break;
        }

    }
将网站的API的地址由"https://free-api.heweather.com/s6/weather/now?parameters&key=f13f32d6c9824100a9b3da90e9ed2944&&location和editText输入的地方两个部分。
使用HttpUrlConnection去发送请求并创建一个String类型保存获取的JSON数据。
这里stringBuffer.toString()就是我们想要的JSon数据。
   @Override
        protected Integer doInBackground(String... strings) {
            try {
                //创建URL——找到网站源
                URL url = new URL(strings[0]);
                //创建开关——HttpURLConnection
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                //创建数据流——Inputstream
                InputStream inputStream = httpURLConnection.getInputStream();
                //创建存放库——InputStreamReader
                InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
                //获取数据——BufferedReader
                BufferedReader bufferedReader = new BufferedReader(reader);

                StringBuffer stringBuffer = new StringBuffer();
                String temp = null;

                while ((temp = bufferedReader.readLine()) != null) {
                    stringBuffer.append(temp);
                }
                bufferedReader.close();
                reader.close();
                inputStream.close();
                //打印出获取数据,看是否正确
                Log.e("wang ", stringBuffer.toString());
                //保存获取的JSON数据
                Json_str=stringBuffer.toString();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return 1;
        }
下面再在String获取数据进行Json数据的解析,和界面UI的更新:
所以可以在onPostExecute()中确保获取数据后,进行数据解析;
 protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            if (integer==1){
                parseJson();
            }
        }
最后在 parseJson()方法中解析JSon的数据,并进行UI的更新设置。
 private void parseJson() {
        try {
            JSONObject jsonObject=new JSONObject(Json_str);
            JSONArray array=jsonObject.getJSONArray("HeWeather6");
            JSONObject obj=array.getJSONObject(0).getJSONObject("basic");
            String string=obj.getString("parent_city");
            JSONObject locaobj2=array.getJSONObject(0).getJSONObject("now");
            String cond=locaobj2.getString("cond_txt");
            int temp=locaobj2.getInt("tmp");
            String fly=locaobj2.getString("wind_dir");
            textView.setText("地区:"+string);
            textView2.setText("天气:"+cond);
            textView3.setText("温度:"+temp);
            textView4.setText("风向:"+fly);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值