您的位置:首页 > 理论基础 > 计算机网络

HttpUrlConnection+Json应用实例(天气预报)

2018-03-15 18:49 357 查看

HttpUrlConnection+ Json应用实例(天气预报)

一.效果图如下:





二.代码如下:

1.布局文件:

<EditText
android:id="@+id/main4_edit"
android:layout_width="match_parent"
android:layout_height="100dp"
android:hint="请输入城市名"/>

<Button
android:id="@+id/main4_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="查询"/>

<TextView
android:id="@+id/main4_text1"
android:layout_width="match_parent"
android:layout_height="60dp" />
<TextView
android:id="@+id/main4_text2"
android:layout_width="match_parent"
android:layout_height="60dp" />
<TextView
android:id="@+id/main4_text3"
android:layout_width="match_parent"
android:layout_height="60dp" />


2.Java文件:

public class Main4Activity extends AppCompatActivity {

private EditText cityET;
private Button button;
private TextView windTV;
private TextView tempTV;
private TextView weatherTV;
private String API = "https://free-api.heweather.com/s6/weather-now?key=f4df0dc926f54557b9c94b6c09451b51&location=";

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

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

}
});
}

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

@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_failed")) {

Toast.makeText(Main4Activity.this, "网络失败", Toast.LENGTH_SHORT).show();
} else {
//解析Json
try {
JSONObject object = new JSONObject(s);
JSONArray array = object.getJSONArray("HeWeather6");
JSONObject object1 = array.getJSONObject(0);
JSONObject nowObj = object1.getJSONObject("now");
String temp = nowObj.getString("tmp");
String wind = nowObj.getString("wind_dir") + nowObj.getString("wind_sc") + "级";
String weather = nowObj.getString("cond_txt");

weatherTV.setText(weather);
windTV.setText(wind);
tempTV.setText(temp);

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

private void bindID() {
//绑定ID
cityET = findViewById(R.id.main4_edit);
button = findViewById(R.id.main4_btn);
windTV = findViewById(R.id.main4_text1);
tempTV = findViewById(R.id.main4_text2);
weatherTV
4000
= findViewById(R.id.main4_text3);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息