【问题标题】:Finding exception while sending data from android app to server [duplicate]从android应用程序向服务器发送数据时发现异常[重复]
【发布时间】:2014-05-04 11:28:56
【问题描述】:

我正在尝试将数据从 Android 应用程序发送到服务器....但是单击保存按钮会导致 android.os.NetworkOnMainThreadException 并且应用程序停止。 请问有人可以帮忙吗?

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://farahkhan.byethost15.com/try.php");
try {

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("username", "01"));
       nameValuePairs.add(new BasicNameValuePair("email", signupemailString));

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       httpclient.execute(httppost);

       signupemail.setText(""); //reset the message text field
        Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

【问题讨论】:

    标签: android networkonmainthread


    【解决方案1】:

    用这个替换你的代码:

    new Thread(new Runnable() {
    
                    @Override
                    public void run() {
                         HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://farahkhan.byethost15.com/try.php");
                    try {
    
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                           nameValuePairs.add(new BasicNameValuePair("username", "01"));
                           nameValuePairs.add(new BasicNameValuePair("email", signupemailString));
    
                           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
                           httpclient.execute(httppost);
                           runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                                 signupemail.setText(""); //reset the message text field
                            Toast.makeText(getBaseContext(),"Sent",Toast.LENGTH_SHORT).show();
    
                            }
                        });
    
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    
                    }
                }).start();
    

    并在清单文件中添加权限:

    <uses-permission android:name="android.permission.INTERNET" /> 
    

    【讨论】:

    • @user3561798 你应该使用AsyncTask
    • @user3561798 :但是 AsyncTask 是更可靠、更清洁的方式来完成这些工作
    • @Arash : 我也用过那个方法……但我无法正确接收或处理它……然后我开始像这样工作……
    【解决方案2】:

    向您的清单文件添加互联网权限

    <uses-permission android:name="android.permission.INTERNET" /> 
    

    之后检查this answer

    【讨论】:

    • 与这个问题无关!
    • 我已经在清单中添加了权限..
    猜你喜欢
    • 2015-03-25
    • 1970-01-01
    • 2011-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多