0

我想将带有两个参数的 POST 请求从 JsonObject 发送到 python 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 输出:http://pastebin.com/GL54UFGG

4

1 回答 1

4

在您的堆栈跟踪中,它清楚地指出:

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

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

于 2013-05-25T22:56:17.643 回答