मैं एक प्रतिक्रिया HTTP GET से एक JSON
वस्तु प्राप्त करना चाहते हैं से एक JSON ऑब्जेक्ट प्राप्त करें:HTTP उत्तर
यहाँ के लिए मेरे वर्तमान कोड है HTTP GET:
protected String doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(params[0]);
HttpResponse response;
String result = null;
try {
response = client.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
// A Simple JSON Response Read
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
// now you have the string representation of the HTML request
System.out.println("RESPONSE: " + result);
instream.close();
if (response.getStatusLine().getStatusCode() == 200) {
netState.setLogginDone(true);
}
}
// Headers
org.apache.http.Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println(headers[i]);
}
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return result;
}
यहाँ convertSteamToString समारोह है :
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
अभी मुझे एक स्ट्रिंग ऑब्जेक्ट मिल रहा है। मैं JSON ऑब्जेक्ट को वापस कैसे प्राप्त कर सकता हूं।
आप अपने परिणाम स्ट्रिंग में क्या प्राप्त कर रहे हैं? – R4chi7
और आप प्रतिक्रिया का उपयोग क्यों कर रहे हैं .getEntity() दो बार? – R4chi7
एक JSON ऑब्जेक्ट वापस पाने के लिए आप जिस JServer को कनेक्ट करते हैं उसे वापस जेएसओएन भेजने के लिए कनेक्ट करते हैं? क्या ऐसा करता है? – rockstar