首先谈一谈大体上思路,使用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();
}
}