获取HttpResponse并解析JSON数据


package com.inesaie.trace.accommon.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class HttpEntity {

public static JSONObject GetHttpEntity(HttpResponse response) {

String line=null;
JSONObject resultJsonObject=null;
StringBuilder entityStringBuilder=new StringBuilder();
try {
BufferedReader b = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"),8*1024);
while ((line=b.readLine())!=null) {
entityStringBuilder.append(line+"/n");
}
//利用从HttpEntity中得到的String生成JsonObject

resultJsonObject = new JSONObject(entityStringBuilder.toString());
} catch (JSONException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
// System.out.println("***httpResponse.getEntity():"+resultJsonObject);

return resultJsonObject;

}

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM