獲取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