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();}}}}
相关文章推荐
- HttpURLConnection+JSON制作天气预报
- 关于HttpUrlConnection和JSON结合使用的案例(天气预报为例)
- 使用HttpURLConnection和JSON天气预报
- 利用HttpUrlConnection和JSON来做简易的天气预报
- HttpUrlConnection+Json应用实例(天气预报)
- HttpURLConnection+AsyncTask+接口回调实现简易get联网请求封装框架
- HttpURLConnection的post请求上传键值对和json数据
- HttpURLConnection和HttpClient分别以GET和POST获取Json数据
- 使用HttpURLConnection和Servlet传输json
- HttpURLconnection使用POST方式提交JSON数据给服务器
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- HttpURLConnection传JSON数据
- HttpURLConnection网络获取天气预报信息
- Android 制作一个网页源代码浏览器(HttpURLConnection)
- 10min实现一个简易网站源码查看器(HttpURLConnection、ScrollView)
- Android通过HttpURLConnection获取JSON并进行UI更新
- HttpURLConnection请求数据(GET)并用JSON解析数据(二)
- Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
- HttpURLConnection请求json数据
- HttpURLConnection post json传参