Android解析聚合数据之天气预报
2016-11-24 10:01
573 查看
免费天气预报API:https://www.juhe.cn/docs/api/id/73 ,申请APPKEY
MainActivity.java
activity_main.xml
MainActivity.java
<span style="font-size:14px;">package com.example.networktest; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private Button sendRequest; private TextView responseText; public static final int SHOW_RESPONSE = 0; private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; // 在这里进行UI操作,将结果显示到界面上 responseText.setText(response); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendRequest = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response); sendRequest.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { sendRequestWithHttpURLConnection(); } }); } protected void sendRequestWithHttpURLConnection() { new Thread() { @Override public void run() { URL url; HttpURLConnection connection = null; try { // url = new // URL("http://10.2.5.119:8080/Server/getData.json"); String cityName = URLEncoder.encode("滨州", "utf-8"); url = new URL( "http://v.juhe.cn/weather/index?format=2&cityname=" + cityName + "&key=ab9d7e2007472d723baf71fcdc4ba094"); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); // 下面对获取到的输入流进行读取 BufferedReader reader = new BufferedReader( new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } System.out.println("response=" + response.toString()); //parseWithJSON(response.toString()); parseWeatherWithJSON(response.toString()); Message message = new Message(); message.what = SHOW_RESPONSE; // 将服务器返回的结果存放到Message中 message.obj = response.toString(); handler.sendMessage(message); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }.start(); } protected void parseWeatherWithJSON(String response) { try { JSONObject jsonObject=new JSONObject(response); String resultcode=jsonObject.getString("resultcode"); if(resultcode.equals("200")){ JSONObject resultObject=jsonObject.getJSONObject("result"); JSONObject todayObject=resultObject.getJSONObject("today"); String date_y=todayObject.getString("date_y"); String week=todayObject.getString("week"); String temperature=todayObject.getString("temperature"); Log.d("MainActivity", "date_y="+date_y+"week="+week+"temp="+temperature); } } catch (JSONException e) { e.printStackTrace(); } } protected void parseWithJSON(String response) { try { JSONArray jsonArray = new JSONArray(response); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String id = jsonObject.getString("id"); String name = jsonObject.getString("name"); String version = jsonObject.getString("version"); Log.d("MainActivity", "id=" + id + "name=" + name + "version=" + version); } } catch (JSONException e) { e.printStackTrace(); } } }</span>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_request" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
相关文章推荐
- Android阶段学习笔记 7.25-7.29 之 解析聚合数据 全国天气预报 Json
- 【Android应用开发】-(17)获取Google天气预报通过Xml和JSON解析数据
- android JSON解析数据 android解析天气预报
- Android程序之全国天气预报查询(聚合数据开发)
- Android程序之全国天气预报查询(聚合数据开发)
- Android程序之聚合数据全国天气预报查询API接口使用演示
- Android解析聚合数据之天气预报
- <WP7>(四)手把手教你写天气预报程序:本地数据库SQL CE,XML数据解析
- Python解析Yahoo的XML格式的天气预报数据
- [置顶] Android网络与数据存储03-在线请求天气API,并解析其中的json数据予以显示
- Gson解析第三方提供Json数据(天气预报,新闻等)
- Android解析中国天气接口JSon数据,应用于天气查询!
- Android之简易天气预报小应用(xml解析练手)
- Android中 GsonFormat插件解析Jason 数据+和风天气接口解析案例
- Android导入外部数据库解析json获取天气预报
- Android 编程之天气预报下来刷新数据及城市容器配置--3
- Qt实现简易天气预报(解析的是json数据)
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析
- Android 天气预报 接口数据获取 from 中国气象网
- <WP7>(三)手把手教你写天气预报程序:运用WebClient获取天气和json数据解析