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

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Demet
  • 61
  • 1
  • 1
  • 5
  • 1
    Sorry Demet I would love to answer your question but you are giving us not a lot to work with. And that note in the most polite way. Just asking for code is not the best behavior. What res – Janusz Aug 04 '11 at 10:26
  • What is the response that you get? Is it null? Is there an Exception thrown? – Janusz Aug 04 '11 at 10:28

4 Answers4

3

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

kishore
  • 1,658
  • 7
  • 24
  • 33
  • 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? – Yi Feng Xie Mar 01 '14 at 18:21
  • @YiFeng you can also use asyncTask feature of android to achieve the same result. Here is a quick link to asyncTask http://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 – kishore Mar 04 '14 at 06:22
1

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.

Shruti
  • 1
  • 13
  • 55
  • 95
  • 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`? – Peterdk Dec 02 '11 at 17:09
  • @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 : http://stackoverflow.com/questions/8365741/is-httpresponse-string/8365974#8365974 – Shruti Dec 03 '11 at 06:30
  • 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. – Peterdk Dec 03 '11 at 14:45
0

This is a permission issue.

Add this line <uses-permission android:name="android.permission.INTERNET" /> to your manifest file and rebuild.

0

First of all try to change the return type to String by doing these 2 steps

Change

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 

to

public static String doPost(String url, JSONObject c) throws ClientProtocolException, IOException 

AND

Change

HttpResponse response; 

to

String response; 

Now check the response string? Is it still null?


Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106