2012-06-25 116 views
-2

我需要的響應轉換成字符串..如何接收JSON響應

 HttpClient httpclient = new DefaultHttpClient(); 
     Log.d("HTTP","Exe"); 
     String url=Sign(token); 
     Log.d("HTTP","Exe"); 

     HttpPost httpPost; 
     httpPost = new HttpPost(url); 

響應(JSON)應該是我如何收到類似

{ 
    "url": "http://db.tt/APqhX1", 
    "expires": "Tue, 01 Jan 2030 00:00:00 +0000" 
} 

我正在研究Android(java)..

回答

2

這是將任何json作爲http響應的方式。我希望這將是有用的。

HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); 

try { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 256); 
StringBuilder sb = new StringBuilder(); 
String line = null; 
while ((line = reader.readLine()) != null) sb.append(line); 
    result = sb.toString(); 
is.close(); 
} catch (Exception e) {} 

JSONArray jArray = new JSONArray(result); 
for(int i = 0; i < jArray.length(); i++) { 
JSONObject json_data = jArray.getJSONObject(i); 
String url= json_data.getString("url"); 
    String expires= json_data.getString("expires"); 
} 
0

有很多例子,如果你google和反正嘗試:

 response = client.execute(httpPost); 
    HttpEntity httpEntity = response.getEntity(); 
    InputStream is = httpEntity.getContent(); 

..將Inputstream轉換爲字符串