json解析天气预报
虚拟机可以用,真机解析失败???解析类:package com.xunfang.demo_weather.test;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.ne
·
虚拟机可以用,真机解析失败???
解析类:
package com.xunfang.demo_weather.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import android.app.Activity;
public class JSON extends Activity
{
public static List<Map<String, String>> getJsonpath(String path)
throws Exception
{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
String json = null;
Map<String, String> map;
URL url = new URL(path);
HttpURLConnection cn = (HttpURLConnection) url.openConnection();
cn.setConnectTimeout(5 * 1000);
cn.setRequestMethod("GET");
System.out.println("stream======="+cn.getInputStream());
InputStreamReader in = new InputStreamReader(cn.getInputStream());
System.out.println("in======" + in);
// 流的应用与读取
BufferedReader bu = new BufferedReader(in);
String line = bu.readLine().toString();
bu.close();
in.close();
// 把字符数组转换成字符串
json = new String(line);
// 这里是以对象的形式
JSONObject item1 = new JSONObject(json);
// 得到对象中的对象
JSONObject item = item1.getJSONObject("weatherinfo");
// System.out.println(path);
// 获取对象中的每一个数值
System.out.println(json);
String name = item.getString("city");
String id = item.getString("cityid");
String temp = item.getString("temp");
String fengxiang = item.getString("WD");
String daxiao = item.getString("WS");
String wet = item.getString("SD");
String time = item.getString("time");
String isreader = item.getString("isRadar");
String Radar = item.getString("Radar");
// 添加到MAP中
map = new HashMap<String, String>();
map.put("name", name);
map.put("id", id);
map.put("temp", temp);
map.put("fengxiang", fengxiang);
map.put("daxiao", daxiao);
map.put("wet", wet);
map.put("time", time);
map.put("isReader", isreader);
map.put("Rader", Radar);
list.add(map);
// 测试数据
for (Map<String, String> list1 : list)
{
System.out.println("测试数据...");
System.out.println(list1.get("name"));
System.out.println(list1.get("id"));
System.out.println(list1.get("temp"));
System.out.println(list1.get("fengxiang"));
System.out.println(list1.get("daxiao"));
}
return list;
}
}
主activity:
package com.xunfang.demo_weather;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.xunfang.demo_weather.test.JSON;
/**
* 解析天气预报
*
* @author smalt
*
*/
public class MainActivity extends Activity {
private Button btnShow;
private TextView textView1, textView2, textView3, textView4;
private LinearLayout layout;
private String UrlApi = "http://toy.weather.com.cn/SearchBox/searchBox?callback=jsonp1342857491709&_=1342857620727&language=zh&keyword=guilin";
private String Url01 = "http://www.weather.com.cn/data/sk/101010100.html";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShow = (Button) findViewById(R.id.btn_get_value);
// 布局框
layout = (LinearLayout) findViewById(R.id.layout);
textView1 = (TextView) findViewById(R.id.tv01);
textView2 = (TextView) findViewById(R.id.tv02);
textView3 = (TextView) findViewById(R.id.tv03);
textView4 = (TextView) findViewById(R.id.tv04);
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("api===" + UrlApi);
// 获取json数据
try {
/*********************************/
/*********************************/
String path = "http://www.weather.com.cn/data/sk/101010100.html";
/*
* {"weatherinfo":{"city":"北京","cityid":"101010100","temp":"-10"
* ,
* "WD":"东北风","WS":"1级","SD":"52%","WSE":"1","time":"22:40",
* "isRadar":"1","Radar":"JC_RADAR_AZ9010_JB"}}
*/
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list = JSON.getJsonpath(path);
for (Map<String, String> list1 : list) {
System.out.println("打印数据...");
System.out.println(list1.get("name"));
System.out.println(list1.get("id"));
System.out.println(list1.get("temp"));
System.out.println(list1.get("fengxiang"));
System.out.println(list1.get("daxiao"));
System.out.println(list1.get("fengxiang"));
textView1.setText("城市 :" + list1.get("name"));
textView2.setText("风向 :" + list1.get("fengxiang"));
textView3.setText(list1.get("temp"));
textView4.setText(list1.get("daxiao"));
// layout.addView(textView1);
// layout.addView(textView2);
// layout.addView(textView3);
// layout.addView(textView4);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("获取失败!");
Log.i("info", "获取失败!");
}
}
});
}
// // 获取数据
// public final static InputStream getStream(String args) {
// InputStream stream = null;
// DefaultHttpClient client = new DefaultHttpClient();
// HttpGet get = new HttpGet(args);
// try {
// HttpResponse response = client.execute(get);
// // 成功获取数据
// if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// HttpEntity entity = response.getEntity();
// stream = entity.getContent();
// }
// return stream;
// } catch (Exception e) {
// e.printStackTrace();
// return stream;
// }
//
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
更多推荐
已为社区贡献8条内容
所有评论(0)