热度 22
2015-12-3 21:49
2224 次阅读|
1 个评论
安卓从百度天气接口获取天气,Http和JSON操作 wxleasyland@sina.com 2015.12 url中的ak=后面要加上百度的key,请自行申请。 关键代码如下,比较简单: import org.json.JSONArray; import org.json.JSONObject; import android.os.Handler; import java.net.URL; import java.net.HttpURLConnection; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; // 百度天气接口JSON示例 String g_str="{\"error\":0,\"status\":\"success\",\"date\":\"2015-11-17\",\"results\": ,\"weather_data\": }]}"; void mygeneratestr() //启动一个线程,进行http连接,整理天气数据 { new Thread(new Runnable() { @Override public void run() { g_str=""; g_str2=""; g_str3=""; g_str4=""; String str=""; HttpURLConnection connection=null; try { URL url=new URL("http://api.map.baidu.com/telematics/v3/weather?location=%E5%8C%97%E4%BA%ACoutput=jsonak="); 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 sb=new StringBuilder(); while((str=reader.readLine())!=null) sb.append(str); g_str+=sb.toString(); } catch(Exception e) { //e.printStackTrace(); g_str=null; g_str2="http出错。错误信息:"+e.toString(); } finally { if(connection!=null) connection.disconnect(); } if(g_str==null) { g_str=""; return;} if(!g_str.contains("\"status\":\"success\"")) { g_str2="http得到的信息不符:"+g_str; g_str=""; return; } try { JSONObject myobj=new JSONObject(g_str); //总OBJECT JSONObject myobj2=myobj.getJSONArray("results").getJSONObject(0); JSONObject myobj31=myobj2.getJSONArray("index").getJSONObject(0);//穿衣 JSONObject myobj32=myobj2.getJSONArray("index").getJSONObject(1);//洗车 JSONObject myobj33=myobj2.getJSONArray("index").getJSONObject(2);//旅游 JSONObject myobj34=myobj2.getJSONArray("index").getJSONObject(3);//感冒 JSONObject myobj35=myobj2.getJSONArray("index").getJSONObject(4);//运动 JSONObject myobj36=myobj2.getJSONArray("index").getJSONObject(5);//紫外线 JSONObject myobj41=myobj2.getJSONArray("weather_data").getJSONObject(0);//今 JSONObject myobj42=myobj2.getJSONArray("weather_data").getJSONObject(1);//明 JSONObject myobj43=myobj2.getJSONArray("weather_data").getJSONObject(2);//后 JSONObject myobj44=myobj2.getJSONArray("weather_data").getJSONObject(3);//大后 g_str2=myobj2.getString("currentCity")+myobj41.getString("date")+"指数"+myobj2.getString("pm25")+"\n" +"今天 "+myobj41.getString("temperature")+" "+myobj41.getString("weather")+" "+myobj41.getString("wind")+"\n" +"明天 "+myobj42.getString("temperature")+" "+myobj42.getString("weather")+" "+myobj42.getString("wind")+"\n" +"后天 "+myobj43.getString("temperature")+" "+myobj43.getString("weather")+" "+myobj43.getString("wind")+"\n" +"大后 "+myobj44.getString("temperature")+" "+myobj44.getString("weather")+" "+myobj44.getString("wind")+"\n" +myobj31.getString("des"); //g_str3=myobj31.getString("des")+"\n" // +myobj34.getString("des")+"\n" // +myobj36.getString("des"); g_str4="今"+myobj41.getString("temperature")+"\n" +"明"+myobj42.getString("temperature"); } catch (Exception e) { g_str=""; g_str2 ="http正常,但JSON 出错"; g_str3 =""; g_str4 =""; //e.printStackTrace(); } } }).start(); }