not getting response response = httpclient.execute(request);

16,136

Solution 1

You should call the method asynchronously. Then it will work with the same code. Add these two lines of code to your project

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

and make minimum sdk version to 9

Solution 2

You can check that if you are getting response from server or not by using following code :

HttpResponse response = httpClient1.execute(request);
                    Log.v("response code", response.getStatusLine()
                            .getStatusCode() + ""); 

If you get the value of response code as 200 then you are getting data from server and if the response code value >=300 then you have error at your server side.

Share:
16,136
Demet
Author by

Demet

Updated on June 04, 2022

Comments

  • Demet
    Demet almost 2 years
    public class HTTPPoster {
        public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
        {
    
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost request = new HttpPost(url);
    
            HttpEntity entity;
            StringEntity s = new StringEntity(c.toString());
            s.setContentEncoding((Header) new BasicHeader(HTTP.DEFAULT_CONTENT_CHARSET, "application/json"));
            entity = s;
            request.setEntity(entity);
    
            HttpResponse response;
    
            response = httpclient.execute(request);
    
            return response;
        }
    
    }
    

    This is the code but on response = http.client.execute(request) doesn't get response. I couldn't find why.

  • Peterdk
    Peterdk over 12 years
    The response is not a HtppResponse but a String. Or how do you get it to be a HttpResponse? Does this depend on the ResponseHandler?
  • Shruti
    Shruti over 12 years
    @Peterdk : please see the following question i asked after your comment.The answer i gave is correct .You kindly update your knowledge.See my question here : stackoverflow.com/questions/8365741/is-httpresponse-string/…
  • Peterdk
    Peterdk over 12 years
    If you supply a responsehandler, the method returns T. So there is also a generic method of execute available, and in case of the BasicResponseHandler it returns a string.
  • Yi Feng Xie
    Yi Feng Xie about 10 years
    many thanks to you! I found all http request example and all not work. After I add your code, all examples work fine! Why these examples not mention your code?
  • kishore
    kishore about 10 years
    @YiFeng you can also use asyncTask feature of android to achieve the same result. Here is a quick link to asyncTask developer.android.com/reference/android/os/AsyncTask.html. You should call your http request inside the doBackgroundTask function. Doing blocking tasks in main thread is not preferred in android. Blocking tasks should be done in background thread
  • user1418706
    user1418706 almost 10 years
    Fantastic answer - very helpful