安卓大json文件解析_Android之JSON格式数据解析

packagecom.rainsong.jsondemo;importjava.io.IOException;importjava.net.URLEncoder;importjava.util.ArrayList;importorg.apache.http.HttpResponse;importorg.apache.http.NameValuePair;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importandroid.content.Context;importandroid.os.AsyncTask;importandroid.widget.TextView;importandroid.widget.Toast;public class QueryTask extends AsyncTask{

Context context;

TextView tv_result;private static final String JUHE_URL_ENVIRONMENT_AIR_PM =

"http://web.juhe.cn:8080/environment/air/pm";private static final String JUHE_APPKEY = "你申请的APPKEY值";publicQueryTask(Context context, TextView tv_result) {//TODO Auto-generated constructor stub

super();this.context =context;this.tv_result =tv_result;

}

@OverrideprotectedString doInBackground(String... params) {

String city= params[0];

ArrayList headerList = new ArrayList();

headerList.add(new BasicNameValuePair("Content-Type", "text/html; charset=utf-8"));

String targetUrl=JUHE_URL_ENVIRONMENT_AIR_PM;

ArrayList paramList = new ArrayList();

paramList.add(new BasicNameValuePair("key", JUHE_APPKEY));

paramList.add(new BasicNameValuePair("dtype", "json"));

paramList.add(new BasicNameValuePair("city", city));for (int i = 0; i < paramList.size(); i++) {

NameValuePair nowPair=paramList.get(i);

String value=nowPair.getValue();try{

value= URLEncoder.encode(value, "UTF-8");

}catch(Exception e) {

}if (i == 0) {

targetUrl+= ("?" + nowPair.getName() + "=" +value);

}else{

targetUrl+= ("&" + nowPair.getName() + "=" +value);

}

}

HttpGet httpRequest= newHttpGet(targetUrl);try{for (int i = 0; i < headerList.size(); i++) {

httpRequest.addHeader(headerList.get(i).getName(),

headerList.get(i).getValue());

}

HttpClient httpClient= newDefaultHttpClient();

HttpResponse httpResponse=httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == 200) {

String strResult=EntityUtils.toString(httpResponse.getEntity());returnstrResult;

}else{return null;

}

}catch(IOException e) {

e.printStackTrace();

}return null;

}

@Overrideprotected voidonPostExecute(String result) {if (result != null) {try{

JSONObject jsonObject= newJSONObject(result);int resultCode = jsonObject.getInt("resultcode");if (resultCode == 200) {

JSONArray resultJsonArray= jsonObject.getJSONArray("result");

JSONObject resultJsonObject= resultJsonArray.getJSONObject(0);

String output= context.getString(R.string.city) + ": " + resultJsonObject.getString("city") + "\n"

+ context.getString(R.string.PM25) + ": " + resultJsonObject.getString("PM2.5") + "\n"

+ context.getString(R.string.AQI) + ": " + resultJsonObject.getString("AQI") + "\n"

+ context.getString(R.string.quality) + ": " + resultJsonObject.getString("quality") + "\n"

+ context.getString(R.string.PM10) + ": " + resultJsonObject.getString("PM10") + "\n"

+ context.getString(R.string.CO) + ": " + resultJsonObject.getString("CO") + "\n"

+ context.getString(R.string.NO2) + ": " + resultJsonObject.getString("NO2") + "\n"

+ context.getString(R.string.O3) + ": " + resultJsonObject.getString("O3") + "\n"

+ context.getString(R.string.SO2) + ": " + resultJsonObject.getString("SO2") + "\n"

+ context.getString(R.string.time) + ": " + resultJsonObject.getString("time") + "\n";

tv_result.setText(output);

}else if (resultCode == 202) {

String reason= jsonObject.getString("reason");

tv_result.setText(reason);

}else{

Toast.makeText(context,"查询失败",

Toast.LENGTH_LONG).show();

tv_result.setText("");

}

}catch(JSONException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}else{

Toast.makeText(context,"查询失败",

Toast.LENGTH_LONG).show();

tv_result.setText("");

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值