Android课堂学习笔记——HttpUrlConnection(四)

HttpUrlConnection+JSON应用实例

获取天气实况

xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lianxiti.WeatherActivity">

    <EditText
        android:id="@+id/weather_et"
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <Button
        android:id="@+id/weather_btn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="开始查询"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="天气:"
            android:textSize="30sp"
            android:gravity="center"/>

        <TextView
            android:id="@+id/weather_tv1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="25sp"/>

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="风力:"
            android:textSize="30sp"
            android:gravity="center"/>

        <TextView
            android:id="@+id/weather_tv2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="25sp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="温度:"
            android:textSize="30sp"
            android:gravity="center"/>

        <TextView
            android:id="@+id/weather_tv3"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:textSize="25sp"/>

    </LinearLayout>

</LinearLayout>

java代码

public class WeatherActivity extends AppCompatActivity {

    private EditText weaEt;
    private Button weaBtn;
    private TextView weaTv1;
    private TextView weaTv2;
    private TextView weaTv3;
    private String weatherAPI = "https://free-api.heweather.com/s6/weather/now?key=1318c609832f407dab8f64b44d6d9160&location=";

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

        bind();

        weaBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String city = weaEt.getText().toString();
                new WeaTask(WeatherActivity.this).execute(weatherAPI+city);
            }
        });

    }

    class WeaTask extends AsyncTask<String,String,String>{

        private Context context;
        public WeaTask(Context context){
            this.context = context;
        }
        @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_fail")){
                Toast.makeText(WeatherActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
            }else {
                try {
                    JSONObject object = new JSONObject(s);
                    JSONArray array = object.getJSONArray("Heweather6");
                    JSONObject object1 = array.getJSONObject(0);
                    JSONObject nowObj = object1.getJSONObject("now");
                    String weather = nowObj.getString("cond_txt");
                    String wind = nowObj.getString("wind_dir")+nowObj.getString("wind_sc")+"级";
                    String temperature = nowObj.getString("tmp");

                    weaTv1.setText(weather);
                    weaTv2.setText(wind);
                    weaTv3.setText(temperature);

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

        }
    }

    private void bind() {
        weaEt = findViewById(R.id.weather_et);
        weaBtn = findViewById(R.id.weather_btn);
        weaTv1 = findViewById(R.id.weather_tv1);
        weaTv2 = findViewById(R.id.weather_tv2);
        weaTv3 = findViewById(R.id.weather_tv3);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值