gpt4 book ai didi

java - Android DefaultHttpClient() 发布请求

转载 作者:行者123 更新时间:2023-12-01 18:48:15 25 4
gpt4 key购买 nike

我想将带有两个参数的 POST 请求从 JsonObject 发送到 python API,该 API 响应 True 或 False,但它根本不起作用...

我的代码:

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.30.0.47:8080/getsession");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Name", db.getString("name")));
nameValuePairs.add(new BasicNameValuePair("Passwort", db.getString("passwort")));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse res = httpclient.execute(httppost);
Log.v("Response", res.toString());

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

LogCat Output: http://pastebin.com/GL54UFGG

最佳答案

在你的堆栈跟踪中它清楚地表明:

05-25 20:42:05.455: E/AndroidRuntime(29706): Caused by: android.os.NetworkOnMainThreadException

这意味着你正在MainUI线程中执行网络负载,你必须使用单独的线程来进行网络工作,最好使用AsyncTask .

关于java - Android DefaultHttpClient() 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16754575/

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