生活随笔
收集整理的這篇文章主要介紹了
解析json获取天气信息(中央气象台)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
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對(duì)象?????HttpGet?httpRequest?=?new?HttpGet(strUrl);?????String?strResult?=?"";?????try?{?????//?HttpClient對(duì)象?????HttpClient?httpClient?=?new?DefaultHttpClient();?????//?獲得HttpResponse對(duì)象?????HttpResponse?httpResponse?=?httpClient.execute(httpRequest);?????if?(httpResponse.getStatusLine().getStatusCode()?==?HttpStatus.SC_OK)?{?????//?取得返回的數(shù)據(jù)?????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;?//返回結(jié)果?????}?????//對(duì)于返回的結(jié)果我們通過(guò)Json解析工具進(jìn)行解析。下面是解析函數(shù)的代碼,其參數(shù)就是要解析的Json格式數(shù)據(jù)字符串。??private?String?parseJson(String?strResult)?{?????try?{?????JSONObject?jsonObj?=?new?JSONObject(strResult).getJSONObject("weatherinfo");??????b?=jsonObj.getString("temp1");?//當(dāng)前日期?????//dayofweek=?jsonObj.getString("week");?//當(dāng)前星期?????//city.setText(jsonObj.getString("city"));?//城市名稱?????//ftime=?jsonObj.getInt("fchh");?//更新時(shí)間(整點(diǎn))【更新時(shí)間確定temp1屬于哪天】??????//由于數(shù)據(jù)較多此處省略了部分代碼,其他數(shù)據(jù)解析方法相同,大家可以照葫蘆畫瓢。?????System.out.println("zhelinn");?????}?catch?(JSONException?e)?{?????Log.i("Erorr","Json?parse?error");?????e.printStackTrace();}?????System.out.println("zheli??v"+b);?????????return?b;??}}?//時(shí)間緊張,就讀取了一個(gè)字符串,其他雷同
? ?
轉(zhuǎn)載于:https://blog.51cto.com/12575213/1186547
總結(jié)
以上是生活随笔為你收集整理的解析json获取天气信息(中央气象台)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。