HttpUrlConnection+JSon应用实例

效果图如下:
这里写图片描述
xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.myapplicatio.MainActivity">

  >
  <EditText
      android:id="@+id/city"
      android:layout_width="match_parent"
      android:layout_height="50dp" />
    <Button
        android:id="@+id/btn"
        android:text="start"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
   <TextView
       android:id="@+id/xianshi1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
   <TextView
       android:id="@+id/xianshi2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
   <TextView
       android:id="@+id/xianshi3"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

</LinearLayout>

Activity代码如下:

package com.example.administrator.myapplicatio;

import android.os.AsyncTask;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;

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

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;



public class MainActivity extends AppCompatActivity {

    private Button button;
    private EditText cityET;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private String Json_str;
    private String API="https://free-api.heweather.com/s6/weather/now?&key=1f9f8a6dc8fe4ab1b9bd15764204a537&location=";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindID();
      button.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              API=API+cityET.getText().toString();
              new MyTask().execute(API);
          }
      });
    }

    private void bindID() {

        button=findViewById(R.id.btn);
        cityET=findViewById(R.id.city);
        textView1=findViewById(R.id.xianshi1);
        textView2=findViewById(R.id.xianshi2);
        textView3=findViewById(R.id.xianshi3);
    }
    class MyTask extends AsyncTask<String,Integer,Integer>{

        @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;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            if (integer==1){
                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");
                    textView1.setText("地区:"+string);
                    textView2.setText("天气:"+cond);
                    textView3.setText("温度:"+temp);

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值