您的位置:首页 > Web前端 > JavaScript

解析json格式的数据

2016-03-19 22:13 375 查看
一、本程序是从客户端发送请求到服务器,服务器返回josn格式的数据,客户端进行解析并展示。

二、代码

public class MainActivity extends Activity {

private EditText ed_city;
private TextView city_result1;
private TextView city_result2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ed_city = (EditText) findViewById(R.id.ed_city);
city_result1 = (TextView) findViewById(R.id.city_result1);
city_result2 = (TextView) findViewById(R.id.city_result2);
}

private final static String PATH = "http://wthrcdn.etouch.cn/weather_mini?city=";

protected static final int SUCCESS = 0;
protected static final int INVALID_CITY = 1;
protected static final int ERROR = 2;

private String city;
String ul;
//<strong>五、handler中进行UI操作</strong>
private Handler mHandler = new Handler(){

public void handleMessage(android.os.Message msg) {

switch (msg.what) {
case SUCCESS:

try {
JSONArray data = (JSONArray) msg.obj;
String day01 = data.getString(0);
city_result1.setText(day01);

String day02 = data.getString(1);
city_result2.setText(day02);
} catch (Exception e) {
e.printStackTrace();
}

break;
case INVALID_CITY:
Toast.makeText(MainActivity.this, "城市无效 ...", 0).show();
break;
case ERROR:

Toast.makeText(MainActivity.this, "网络 问题 .... ...", 0).show();
break;

default:
break;
}
};
};

public void searchCityWeather(View v){

city = ed_city.getText().toString().trim();
//<strong>一、点击按钮 从EditText中获取要查询的城市</strong>
if(TextUtils.isEmpty(city)){
Toast.makeText(this, "路径错误...", 0).show();
return;
}

//http://wthrcdn.etouch.cn/weather_mini?city=%E6%B7%B1%E5%9C%B3

// <strong>二、发起请求 给 那个 网站了</strong>
new Thread(){
public void run() {

try {

ul = PATH + URLEncoder.encode(city, "UTF-8");
URL url = new URL(ul);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// 设置 必要的参数信息
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");

//判断 响应码
int code = conn.getResponseCode();

if(code==200){

InputStream in = conn.getInputStream();
String data = StreamTool.decodeStream(in);//StreamTool自定义的工具类,下面附代码

//System.out.println(data);

//<strong>三、解析json 格式的数据 </strong>
JSONObject jsonObj = new JSONObject(data);

// 获得 desc 的值
String result = jsonObj.getString("desc");
if("OK".equals(result)){

//城市 有效, 返回了需要的数据
JSONObject dataObj = jsonObj.getJSONObject("data");

JSONArray jsonArray = dataObj.getJSONArray("forecast");

//<strong>四、通知 更新 ui sendMessage到Handler</strong>
Message msg = Message.obtain();
msg.obj = jsonArray;
msg.what=SUCCESS;
mHandler.sendMessage(msg);

/*	String value1 = jsonArray.getString(0);
String value2 = jsonArray.getString(2);
System.out.println(value1);
System.out.println(value2);*/

}else{

//城市 无效
Message msg = Message.obtain();
msg.what=INVALID_CITY;
mHandler.sendMessage(msg);
}
}

} catch (Exception e) {
e.printStackTrace();

Message msg = Message.obtain();
msg.what=ERROR;
mHandler.sendMessage(msg);
}

};
}.start();

}

}


三、工具类代码

public class StreamTool {

public static String decodeStream(InputStream in) throws IOException {

// 底层流
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int len =0;
byte[] buf = new byte[1024];

while((len=in.read(buf))>0){
baos.write(buf, 0, len);
}

String data = baos.toString();

return data;
}

}


四、json数据

{
"desc": "OK",
"status": 1000,
"data": {
"wendu": "22",
"ganmao": "天气转凉,空气湿度较大,较易发生感冒,体质较弱的朋友请注意适当防护。",
"forecast": [
{
"fengxiang": "无持续风向",
"fengli": "微风级",
"high": "高温 27℃",
"type": "多云",
"low": "低温 20℃",
"date": "19日星期六"
},
{
"fengxiang": "无持续风向",
"fengli": "微风级",
"high": "高温 25℃",
"type": "阵雨",
"low": "低温 18℃",
"date": "20日星期天"
},
{
"fengxiang": "无持续风向",
"fengli": "微风级",
"high": "高温 21℃",
"type": "小雨",
"low": "低温 18℃",
"date": "21日星期一"
},
{
"fengxiang": "无持续风向",
"fengli": "微风级",
"high": "高温 21℃",
"type": "小雨",
"low": "低温 19℃",
"date": "22日星期二"
},
{
"fengxiang": "无持续风向",
"fengli": "微风级",
"high": "高温 22℃",
"type": "雷阵雨",
"low": "低温 18℃",
"date": "23日星期三"
}
],
"yesterday": {
"fl": "微风",
"fx": "无持续风向",
"high": "高温 24℃",
"type": "多云",
"low": "低温 19℃",
"date": "18日星期五"
},
"aqi": "55",
"city": "深圳"
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: