Android 发布请求

发布于 2024-12-11 19:14:41 字数 851 浏览 0 评论 0原文

我有 javascript 代码,我试图在 android 应用程序中模仿:

这是 javascript 代码:

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

这是我到目前为止的 android 应用程序(不起作用):

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); 

当我尝试在 eclipse 上调试此代码时,模拟器会保留在调试器挂起时运行。谢谢!

注意:它挂在 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

那支青花 2024-12-18 19:14:41

这是我用于 Android post 请求的代码:

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);

...等等。

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.

谈场末日恋爱 2024-12-18 19:14:41

尝试一下:

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();
}

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();
}
小巷里的女流氓 2024-12-18 19:14:41

您是否想将 HttpPost 路径设置为 path。我认为您挂起是因为您没有给 HttpPost 提供有效的 URL。您需要将此行修改

HttpPost httppost = new HttpPost("path"); 

为类似的内容

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

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"); 
岁月染过的梦 2024-12-18 19:14:41

与 JS 版本相比,您的文本字符串的开头和结尾有额外的语音标记吗?

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

仙女 2024-12-18 19:14:41
 // 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);
    }
} 
 // 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);
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文