HttpResponse来stringandroid

我试图隐藏http响应string使用下面的代码,但我的响应string正在终止任何想法,如何将http响应转换为string,以便我没有得到任何缓冲区问题。

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(); } 

任何人的任何build议,请帮助。 谢谢Deepesh

我认为,有一个更简单的方法:

 String result = EntityUtils.toString(resp_entity); 

对?

获取响应InputStream像这样:

 httpResponse = client.execute(request); HttpEntity entity = httpResponse.getEntity(); InputStream is = entity.getContent(); 

试试这个方法:

 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(); } 
 HttpResponse response = http.execute(httpMethod); int responseCode = response.getStatusLine().getStatusCode(); switch(responseCode) { case 200: HttpEntity entity = response.getEntity(); if(entity != null) { String responseBody = EntityUtils.toString(entity); } break; } 

请参阅http://cdrussell.blogspot.com.es/2011/12/android-get-body-of-http-response-as.html

 HttpPost httppost; DefaultHttpClient httpclient; ResponseHandler <String> res=new BasicResponseHandler(); List<NameValuePair> nameValuePairs; String bytesSent; httppost = new HttpPost(URL OF YOUR SITE); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setContentCharset(params, "UTF-8"); httpclient = new DefaultHttpClient(params); nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("key1", value1)); nameValuePairs.add(new BasicNameValuePair("key2", value2)); nameValuePairs.add(new BasicNameValuePair("key3", value3)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); bytesSent = httpclient.execute(httppost, res); 

这是使用这个HTTPPOST的代码,你可以在String中得到响应。

如果必须将查询string传递给服务器,则可以使用nameValuePairs

如果你正在寻找一个更健壮的方式来处理编码和gzip的响应,试试这个:

 String encoding = EntityUtils.getContentCharSet(response.getEntity()); encoding = encoding == null ? "UTF-8" : encoding; InputStream stream = AndroidHttpClient.getUngzippedContent(response.getEntity()); InputStreamEntity unzEntity = new InputStreamEntity(stream,-1); String response = EntityUtils.toString(unzEntity, encoding); 

有人可以试试这个。 我testing了它,并正确运行。

  public String convertHttpResponseToString(HttpResponse res) { InputStream is=null; String responseString=""; try { is=res.getEntity().getContent(); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is)); String line=""; StringBuffer sb=new StringBuffer(); while ((line=bufferedReader.readLine())!=null) { sb.append(line); } responseString=sb.toString(); } catch (Exception e) { // handle Exception here... } return responseString; } 

如果你有响应而不是HttpResponse。你可以通过这种方式获得状态码。 谢谢

  final int statusCode = response.code();