gpt4 book ai didi

java - 安卓发布请求

转载 作者:太空狗 更新时间:2023-10-29 16:23:40 25 4
gpt4 key购买 nike

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

这是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) 上

最佳答案

这是我用于 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);

...等等。

关于java - 安卓发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7880514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com