Skip to content Skip to sidebar Skip to footer

How To Use Http Post Method To Call Php Webservice In Android?

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.apach

Solution 1:

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

responce.getEntity()

Read more here.

Solution 2:

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

HttpEntityentity= response.getEntity();
finalInputStreaminputStream= entity.getContent();

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

If you need the String value of the response entity:

HttpEntityentity= response.getEntity();
finalStringresponseText= EntityUtils.toString(entity);

Solution 3:

HI Mehul,

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

Example

HttpPostpostMethod=newHttpPost(Your Url);
List<NameValuePair> nameValuePairs = newArrayList<NameValuePair>();

nameValuePairs.add(newBasicNameValuePair("key", your value to pass on server));
DefaultHttpClienthc=newDefaultHttpClient();

HttpResponseresponse= hc.execute(postMethod);
HttpEntityentity= response.getEntity();

InputStreaminStream= entity.getContent();

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

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

}

Solution 4:

Perhaps you want to do something similar to the following.

HttpResponseresponse= client.execute(request);
  StatusLinestatus= response.getStatusLine();
  if (status.getStatusCode() == HttpStatus.SC_OK)
  {
    ResponseHandler<String> responseHandler = newBasicResponseHandler();
    StringresponseBody= responseHandler.handleResponse(response);
    ...
  }

Post a Comment for "How To Use Http Post Method To Call Php Webservice In Android?"