最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Android post request - Stack Overflow

programmeradmin2浏览0评论

I have javascript code that i am trying to mimic in an android application:

Here is the javascript code:

text = '{"username":"Hello","password":"World"}';
x.open("POST", url);
x.setRequestHeader("Content-type", "application/json");
x.setRequestHeader("Content-length", text.length);
x.send(text);

and here is what i have so far for the android application(doesnt work):

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url);                   
httppost.setHeader("Content-type", "application/json");
String text = "\"{\"username\":\"Hello\",\"password\":\"World\"}\"";
httppost.setHeader("Content-length",Integer.toString(text.length()));
httppost.setEntity(new StringEntity(text));
HttpResponse response = httpclient.execute(httppost); 

when i try to debug this code on eclipse the emulater keeps running while the debugger hangs. Thanks!

Note: its hanging on httpclient.execute(httppost)

I have javascript code that i am trying to mimic in an android application:

Here is the javascript code:

text = '{"username":"Hello","password":"World"}';
x.open("POST", url);
x.setRequestHeader("Content-type", "application/json");
x.setRequestHeader("Content-length", text.length);
x.send(text);

and here is what i have so far for the android application(doesnt work):

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url);                   
httppost.setHeader("Content-type", "application/json");
String text = "\"{\"username\":\"Hello\",\"password\":\"World\"}\"";
httppost.setHeader("Content-length",Integer.toString(text.length()));
httppost.setEntity(new StringEntity(text));
HttpResponse response = httpclient.execute(httppost); 

when i try to debug this code on eclipse the emulater keeps running while the debugger hangs. Thanks!

Note: its hanging on httpclient.execute(httppost)

Share Improve this question edited Apr 15, 2013 at 21:31 Xavi 20.5k14 gold badges74 silver badges64 bronze badges asked Oct 24, 2011 at 19:03 Mike GMike G 4,96911 gold badges51 silver badges77 bronze badges 4
  • Can you tell what line it's hanging on? – Kurtis Nusbaum Commented Oct 24, 2011 at 19:07
  • @KurtisNusbaum last line of the second snippet – Mike G Commented Oct 24, 2011 at 19:12
  • Have you verified the emulators connectivity to the internet? – Kurtis Nusbaum Commented Oct 24, 2011 at 19:17
  • @Mike G do not set "Content-length" header ... it will be done for ya via StringEntity – Selvin Commented Oct 24, 2011 at 20:36
Add a ment  | 

5 Answers 5

Reset to default 3

Here is the code I use for Android post requests:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("fullurl");

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("parameter", "variable");
post.setEntity (new UrlEncodedFormEntity(pairs));

HttpResponse response = client.execute(post);

...and so on.

Try it out:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url);  
JSONObject json = new JSONObject();
try{
      json.put("username", "Hello");
      json.put("password", "World");
      StringEntity se = new StringEntity(json.toString());  
      se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
      post.setEntity(se);
      HttpResponse response = httpclient.execute(httppost); 
      /*Checking response */
      if(response!=null){
          InputStream in = response.getEntity().getContent(); //Get the data in the entity

}
catch(Exception e){
    e.printStackTrace();
}

Did you mean to set your HttpPost path to just path. I think your hanging because you haven't given the HttpPost a valid URL. You'll need to modify this line:

HttpPost httppost = new HttpPost("path"); 

to something like

HttpPost httppost = new HttpPost("actual/url/path"); 

You have extra speech marks within the start and end of your text string pared to the JS version?

 // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(StringUrl);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));



        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        System.out.println("rep => " + response);


    } catch (IOException e) {
       System.out.println(e);
    }
} 
发布评论

评论列表(0)

  1. 暂无评论