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.
Author by
Demet
Updated on June 04, 2022Comments
-
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 over 12 yearsThe response is not a
HtppResponse
but aString
. Or how do you get it to be aHttpResponse
? Does this depend on theResponseHandler
? -
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 over 12 yearsIf you supply a
responsehandler
, the method returnsT
. So there is also a generic method ofexecute
available, and in case of theBasicResponseHandler
it returns a string. -
Yi Feng Xie about 10 yearsmany 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 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 almost 10 yearsFantastic answer - very helpful