How to use http post method to call php webservice

2020-04-15 11:16发布

I'm calling php webservice through http post method. I'm sending the request in the proper way, but when responce comes, it is not giving me response.

That's what I have:

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

Help me, please.

4条回答
祖国的老花朵
2楼-- · 2020-04-15 11:55

Perhaps you want to do something similar to the following.

  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);
    ...
  }
查看更多
男人必须洒脱
3楼-- · 2020-04-15 12:07

Let's say your org.apache.http.message.BasicHttpResponse@4057fb48 is called response.
To retrieve the data from the HttpResponse, you need:

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

You handle this InputStream depending on what kind of data it contains.

If you need the String value of the response entity:

HttpEntity entity = response.getEntity();
final String responseText = EntityUtils.toString(entity);
查看更多
Emotional °昔
4楼-- · 2020-04-15 12:18

HI Mehul,

Please pass your httpConnection object's getInputStream in this function it will return the response in String.

Example

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

Now Pass this inStream into function it will return the Message of your response.

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

}

查看更多
一纸荒年 Trace。
5楼-- · 2020-04-15 12:21

This is the normal responce. What you need to get the information is to call a method:

responce.getEntity()

Read more here.

查看更多
登录 后发表回答