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; }} //时间紧张,就读取了一个字符串,其他雷同
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。