- 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;
- }}
- //时间紧张,就读取了一个字符串,其他雷同