如何使用http post方法在android中调用php webservice?

时间:2011-05-05 07:04:36

标签: android web-services httpresponse

我通过http post方法调用php webservice。我正在以正确的方式发送请求,但是当回复时,它没有给我回复。

这就是我所拥有的:

org.apache.http.message.BasicHttpResponse@4057f498

请帮帮我。

4 个答案:

答案 0 :(得分:2)

这是正常的反应。获取信息所需的是调用方法:

responce.getEntity()

了解更多here

答案 1 :(得分:1)

HI Mehul,

请在此函数中传递httpConnection对象的getInputStream,它将返回String中的响应。

实施例

HttpPost postMethod = new HttpPost(Your Url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

nameValuePairs.add(new BasicNameValuePair("key", your value to pass on server));
DefaultHttpClient hc = new DefaultHttpClient();

HttpResponse response = hc.execute(postMethod);
HttpEntity entity = response.getEntity();

InputStream inStream = entity.getContent();

现在将此inStream传递给函数,它将返回您的响应消息。

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

}

答案 2 :(得分:1)

假设您的org.apache.http.message.BasicHttpResponse@4057fb48被称为response 要从HttpResponse检索数据,您需要:

HttpEntity entity = response.getEntity();
final InputStream inputStream = entity.getContent();

您可以根据其中包含的数据来处理此InputStream

如果您需要响应实体的String值:

HttpEntity entity = response.getEntity();
final String responseText = EntityUtils.toString(entity);

答案 3 :(得分:0)

也许你想做类似以下的事情。

  HttpResponse response = client.execute(request);
  StatusLine status = response.getStatusLine();
  if (status.getStatusCode() == HttpStatus.SC_OK)
  {
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = responseHandler.handleResponse(response);
    ...
  }