您的位置:首页 > 理论基础 > 计算机网络

HttpURLConnection请求数据(GET)并用JSON解析数据(二)

2015-02-10 17:39 471 查看
new Thread(new Runnable(){
@Override
public void run() {
String u = "http://182.254.155.223/HYMS_INTF/InteMgrController/excuteInteMgrResult.do?reqStr=%7B%22inteId%22%3A%227%22%2C%22expectedDt%22%3A%22%22%2C%22customerId%22%3A%2213382081268%22%2C%22tableTpId%22%3A%22%22%2C%22status%22%3A%22%22%2C%22pageRow%22%3A%2210%22%2C%22pageNum%22%3A%221%22%7D";
HttpURLConnection conn = null;
try {
URL url = new URL(u);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
StringBuilder  sb = new StringBuilder();
if(conn.getResponseCode()==200){
InputStream is  = conn.getInputStream();
//面对获取的输入流进行读取
BufferedReader br =  new BufferedReader(new InputStreamReader(is));
String line;
while((line=br.readLine())!=null){
sb.append(line);
}

}
JSONObject jsonObj = new JSONObject(sb.toString());//把String对象转为JSONObject对象
holder = new Holder();
holder.RESULT_MSG = jsonObj.optString("RESULT_MSG");
holder.RESULT_CODE=jsonObj.optString("RESULT_CODE");
holder.RESULT_MAP =jsonObj.optString("RESULT_MAP");
JSONObject jsonObj1 = new JSONObject(holder.RESULT_MAP);

//下面就说JSONArray这里只解析2个数据
JSONArray jsonArray =jsonObj1.optJSONArray("RESULT_LIST");//找个地方要注意
list = new ArrayList<Holder>();
for(int i=0;i<jsonArray.length();i++){
holder = new Holder();
JSONObject objj = jsonArray.optJSONObject(i);
holder.ORDER_ID = objj.optString("ORDER_ID");
holder.CONFIRM_DT = objj.optString("CONFIRM_DT");
list.add(holder);
}
Message msg = new Message();
msg.what=1;
msg.obj =list;
handler.sendMessage(msg);
//					JSONObject obje = jsonArray.optJSONObject(0);//这里只拿出下标为0的进行解析
//					holder.ORDER_ID = obje.optString("ORDER_ID");
//					holder.CONFIRM_DT = obje.optString("CONFIRM_DT");
//					Message msg = new Message();
//					msg.what=1;
//					msg.obj =holder;
//					handler.sendMessage(msg);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}).start();


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息