Android 发布请求
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我用于 Android post 请求的代码:
...等等。
Here is the code I use for Android post requests:
...and so on.
尝试一下:
Try it out:
您是否想将 HttpPost 路径设置为
path
。我认为您挂起是因为您没有给 HttpPost 提供有效的 URL。您需要将此行修改为类似的内容
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:to something like
与 JS 版本相比,您的文本字符串的开头和结尾有额外的语音标记吗?
You have extra speech marks within the start and end of your text string compared to the JS version?