生活随笔
收集整理的這篇文章主要介紹了
解析json获取天气信息(中央气象台)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
private?String?b;?new?Thread(new?Runnable()?{?????????????public?void?run()?{??????????????????String?a=?connServerForResult("http://m.weather.com.cn/data/101090101.html");?????????????????final?String?v=?"石家莊?"+parseJson(a);?????????????????System.out.println("zheliv"+v);?????????????????tq.post(new?Runnable(){?????????????????????public?void?run(){?????????????????????????tq.setText(v.replace("?","\n"));??????????????????????????????System.out.println("zheliv"+v);?????????????????????}});}}).start();}????private?String?connServerForResult(String?strUrl)?{?????//獲取HttpGet對象?????HttpGet?httpRequest?=?new?HttpGet(strUrl);?????String?strResult?=?"";?????try?{?????//?HttpClient對象?????HttpClient?httpClient?=?new?DefaultHttpClient();?????//?獲得HttpResponse對象?????HttpResponse?httpResponse?=?httpClient.execute(httpRequest);?????if?(httpResponse.getStatusLine().getStatusCode()?==?HttpStatus.SC_OK)?{?????//?取得返回的數據?????strResult?=?EntityUtils.toString(httpResponse.getEntity());?????System.out.println("zheli");?????}?????}?catch?(ClientProtocolException?e)?{?????e.printStackTrace();?????}?catch?(IOException?e)?{?????e.printStackTrace();?????}?????Log.i("Infor",?strResult);?????return?strResult;?//返回結果?????}?????//對于返回的結果我們通過Json解析工具進行解析。下面是解析函數的代碼,其參數就是要解析的Json格式數據字符串。??private?String?parseJson(String?strResult)?{?????try?{?????JSONObject?jsonObj?=?new?JSONObject(strResult).getJSONObject("weatherinfo");??????b?=jsonObj.getString("temp1");?//當前日期?????//dayofweek=?jsonObj.getString("week");?//當前星期?????//city.setText(jsonObj.getString("city"));?//城市名稱?????//ftime=?jsonObj.getInt("fchh");?//更新時間(整點)【更新時間確定temp1屬于哪天】??????//由于數據較多此處省略了部分代碼,其他數據解析方法相同,大家可以照葫蘆畫瓢。?????System.out.println("zhelinn");?????}?catch?(JSONException?e)?{?????Log.i("Erorr","Json?parse?error");?????e.printStackTrace();}?????System.out.println("zheli??v"+b);?????????return?b;??}}?//時間緊張,就讀取了一個字符串,其他雷同
? ?
轉載于:https://blog.51cto.com/12575213/1186547
總結
以上是生活随笔為你收集整理的解析json获取天气信息(中央气象台)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。