HttpUrlconnection请求JSON

以下是WeatherActivity 中的代码

package ssqcom.administrator.json;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

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 WeatherActivity extends AppCompatActivity {

    private TextView tv1;
    private TextView tv2;
    private TextView tv3;
    private EditText editText;
    private Button button;
    private String API="https://free-api.heweather.com/s6/weather/now?key=c9bb36eb94d54d15900d11900c5fa454&location=";

    private Context context;

    public WeatherActivity(Context context){
        this.context=context;
    }

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

        bindID();

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


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

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

            try {
                URL url =new URL(strings[0]);
                HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                InputStream inputStream =null;
                if (httpURLConnection.getResponseCode()==200){
                    inputStream=httpURLConnection.getInputStream();
                }

                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(WeatherActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
            }else {
                try {
                    JSONObject jsonObject=new JSONObject(s);
                    JSONArray jsonArray=jsonObject.getJSONArray("HeWeather6");
                    JSONObject jsonObject1=jsonArray.getJSONObject(0);
                    JSONObject jsonObject2= jsonObject1.getJSONObject("now");
                    String weather =jsonObject2.getString("cond_txt");
                    String wind =jsonObject2.getString("wind_dir")+jsonObject2.getString("wind_sc")+"级";
                    String temperature=jsonObject2.getString("tmp");

                    tv1.setText(weather);
                    tv2.setText(wind);
                    tv3.setText(temperature);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }





    private void bindID() {
        tv1=findViewById(R.id.weahter_tv1);
        tv2=findViewById(R.id.weahter_tv2);
        tv3=findViewById(R.id.weahter_tv3);
        editText=findViewById(R.id.weahter_et);
        button=findViewById(R.id.weather_btn);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值