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

HttpURLConnection和Json制作简易版天气预报

2018-03-15 17:21 337 查看
一、效果展示:二、首先创建一个activity,修改里面的布局文件,添加控件,设置id

       

<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="lenovo.wangmengyuan.json.WeatherActivity">

<EditText
android:hint="请输入城市名"
android:id="@+id/city_et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="查询"
android:id="@+id/search_btn"
android:layout_width="match_parent"
android:layout_height="50dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/tianqi"
android:textSize="30sp"
android:text="天气:"
android:layout_width="match_parent"
android:layout_height="50dp" />

<TextView
android:id="@+id/tianqi1"
android:textSize="30sp"
android:text="温度:"
android:layout_width="match_parent"
android:layout_height="50dp" />

<TextView
android:id="@+id/tianqi2"
android:textSize="30sp"
android:text="风力:"
android:layout_width="match_parent"
android:layout_height="50dp" />

</LinearLayout>

</LinearLayout>
三、在activity中进行绑定控件,设置监听,在内部类中完成Http请求和Json解析,完成简易版天气预报       public class WeatherActivity extends AppCompatActivity {private TextView weatherTV;private TextView wingTV;private TextView tempTV;private Button serrchBtn;private EditText cityET;private String weatherAPI = "https://free-api.heweather.com/s6/weather/now?key=86a3c4999f6346248511a308d60856cd&location=";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_weather);bindID();serrchBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {String city = weatherAPI+cityET.getText().toString();new MyTAsk().execute(city);}});}private void bindID() {cityET = findViewById(R.id.city_et);serrchBtn = findViewById(R.id.search_btn);weatherTV = findViewById(R.id.tianqi);wingTV = findViewById(R.id.tianqi2);tempTV = findViewById(R.id.tianqi1);}class MyTAsk extends AsyncTask<String, String, String> {StringBuffer stringBuffer = new StringBuffer();@Overrideprotected String doInBackground(String... strings) {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_failed";}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 (IOException e) {e.printStackTrace();}return stringBuffer.toString();}@Overrideprotected void onPostExecute(String s) {super.onPostExecute(s);if (s.equals("network_failed")) {Toast.makeText(WeatherActivity.this, "网络失败", Toast.LENGTH_SHORT).show();} else {//Json解析try {JSONObject object = new JSONObject(s);JSONArray array = object.getJSONArray("HeWeather6");JSONObject object1 = array.getJSONObject(0);JSONObject object2 = object1.getJSONObject("now");String weather = object2.getString("cond_txt");String wind = object2.getString("wind_dir") + object2.getString("wind_sc") + "级";String temp = object2.getString("tmp");weatherTV.setText(weather);wingTV.setText(wind);tempTV.setText(temp);} catch (JSONException e) {e.printStackTrace();}}}}
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  anroid