json response java,HTTPResponse为Java中的JSON

I was trying to get an JSONObject from a HTTP response.

try

{

GetMethod postMethod = new GetMethod();

postMethod.setURI(new URI(url, true));

postMethod.setRequestHeader("Accept", "application/json");

httpClient.executeMethod(postMethod);

String resp=postMethod.getResponseBodyAsString();

org.json.JSONTokener tokener = new org.json.JSONTokener(resp);

finalResult = new org.json.JSONArray(tokener);

return finalResult;

}

But I got a runtime warning as

Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

Should I get the response as stream as suggested by the JVM ? If so, how could I parse the JSON from it ?

解决方案

if you want to send jsonObjects from server suppose (tomcat server)

For server side-

creating jsonobjects-

I have Called toJson() for creating jsonobjects this is the implementation-

final JSONObject arr = new JSONObject();

for (int i = 0; i < contactStatus.size(); i++) {

ContactStatus contactObject = contactStatus.get(i);

try {

arr.put(String.valueOf(i), toJson(value1, value2,, value3));

} catch (JSONException e) {

catch block e.printStackTrace();

}

}

//Here we serialize the stream to a String.

final String output = arr.toString();

response.setContentLength(output.length());

out.print(output);//out is object of servlet output stream.

public static Object toJsonForContact(String value1, boolean value2, double value3) throws JSONException {

JSONObject contactObject = new JSONObject();

contactObject.put("id", id);

contactObject.put("status", value1);

contactObject.put("distance", value2);

contactObject.put("relation", value3);

return contactObject;

}

so your jsonobjects are ready for sending we write these objects to ServletoutputStream.

in client side-

while ((ReadResponses = in.readLine()) != null) {

Constants.Response_From_server = ReadResponses;

if (Constants.Response_From_server.startsWith("{")) {

ListOfContactStatus = new ArrayList();

ContactStatus contactStatusObject;

try {

JSONObject json = new JSONObject(Constants.Response_From_server);

for (int i = 0; i < json.length(); i++) {

contactStatusObject = new ContactStatus();

JSONObject json1 = json.getJSONObject(String.valueOf(i));

System.out.println("" + json1.getString("id"));

System.out.println("" + json1.getBoolean("status"));

System.out.println("" + json1.getDouble("distance"));

contactStatusObject.setId(json1.getString("id"));

contactStatusObject.setStatus(json1.getBoolean("status"));

contactStatusObject.setDistance((float) json1.getDouble("distance"));

ListOfContactStatus.add(contactStatusObject);

System.out.println("HTTPTransport:sendMessage Size of ListOfContactStatus" + ListOfContactStatus.size());

}

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值