How to use http post method to call php webservice in android?

后端 未结 4 1843
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 04:27

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 h

4条回答
  •  孤独总比滥情好
    2021-01-22 05:20

    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 nameValuePairs = new ArrayList();
    
    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();
    

    }

提交回复
热议问题