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); } }
相关文章推荐
- Android--HttpUrlConnection+JSON应用实例 (2)
- HttpUrlConnection+JSON应用实例
- HttpUrlConnection+JSon应用实例
- HttpUrlConnection+JSON应用实例
- HttpURLConnection和Json制作简易版天气预报
- Android进阶——HTTPURLConnection&JSON实例
- 关于HttpUrlConnection和JSON结合使用的案例(天气预报为例)
- 使用HttpURLConnection和JSON天气预报
- Android之HttpURLConnection应用实例:武大教务系统获取课表
- HttpURLConnection+JSON的使用实例
- HttpURLConnection+JSON制作天气预报
- Android的网络应用-使用HttpURLConnection
- 配置了拦截器的应用执行HttpURLConnection之connect方法遇到的问题
- Android代码(Handler的运用),HttpURLConnection的应用,将url图片地址转换成图片。
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android HttpURLConnection应用技巧分享
- 关于Android使用HttpURLConnection传输含有中文JSON数据的报错、乱码问题
- Java 通过HttpURLConnection Post方式提交json,并从服务端返回json数据
- HttpURLConnection网络获取天气预报信息
- 模拟手机客户端与Web应用的交互[URL和HttpURLConnection的使用]