Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误android.os.NetworkOnMainThreadException_Java_Android - Fatal编程技术网

Java 错误android.os.NetworkOnMainThreadException

Java 错误android.os.NetworkOnMainThreadException,java,android,Java,Android,我试图创建一个表单,当我们点击按钮时,它会发送到数据库 在运行我的代码时,一切正常,但当我尝试注册表单时,我会收到错误消息:error android.os.NetworkOnMainThreadException 你能帮我吗 package com.example.pst; import java.util.ArrayList; //import android.support.v7.app.ActionBarActivity; import android.os.Bundle;

我试图创建一个表单,当我们点击按钮时,它会发送到数据库 在运行我的代码时,一切正常,但当我尝试注册表单时,我会收到错误消息:error android.os.NetworkOnMainThreadException

你能帮我吗

    package com.example.pst;

import java.util.ArrayList;

//import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;


import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;   
import org.apache.http.message.BasicNameValuePair;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

    public class MainActivity extends Activity {

        EditText nom,prenom,email,prevente ;
        Button bouton ;
        HttpPost httppost;
        StringBuffer buffer;
        HttpClient httpclient ;

        @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.formulaire);

     nom = (EditText)  findViewById(R.id.editTextFormulaire1) ;
     prenom = (EditText) findViewById(R.id.editTextFormulaire2);
     email = (EditText) findViewById(R.id.editTextFormulaire3);
     prevente = (EditText) findViewById(R.id.editTextFormulaire4);
     bouton = (Button) findViewById(R.id.buttonFormulaire1) ;

     String enregistre = bouton.getText().toString();

     bouton.setOnClickListener(new View.OnClickListener()
     {
         public void onClick(View nouveau)

         {
            params[0] = null; 
            new AsyncTask<Void, Void, Void>() {
                @Override

                protected Void doInBackground(Void... params) {


                    httpclient = new DefaultHttpClient();
                    httppost = new HttpPost("http://10.0.2.2:8080");

                      try {
                          ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                          nameValuePairs.add(new BasicNameValuePair("nom", N));
                          nameValuePairs.add(new BasicNameValuePair("prenom", P));
                          nameValuePairs.add(new BasicNameValuePair("email", E));
                          nameValuePairs.add(new BasicNameValuePair("prevente", B));

                          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                  
                          HttpResponse response = httpclient.execute(httppost);
                          Log.i("postData", response.getStatusLine().toString());
                           }
                           catch(Exception e)
                           {
                               Log.e("log_tag", "Error:  "+e.toString());
                           }  
                         }
                     };

                    return null;
              }
            }.execute();
           }
          }
package com.example.pst;
导入java.util.ArrayList;
//导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.app.Activity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
公共类MainActivity扩展了活动{
编辑文本名称、prenom、电子邮件、预防;
钮扣鲍顿;
HttpPost-HttpPost;
字符串缓冲区;
HttpClient-HttpClient;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.formulaire);
nom=(EditText)findViewById(R.id.editTextFormulaire1);
prenom=(EditText)findViewById(R.id.editTextFormulaire2);
电子邮件=(EditText)findViewById(R.id.editTextFormulaire3);
prevente=(EditText)findViewById(R.id.editTextFormulaire4);
bouton=(按钮)findViewById(R.id.buttonFormulaire1);
字符串enregistre=bouton.getText().toString();
setOnClickListener(新视图.OnClickListener()
{
公共空间点击(新观点)
{
参数[0]=null;
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://10.0.2.2:8080");
试一试{
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“nom”,N));
添加(新的BasicNameValuePair(“prenom”,P));
添加(新的BasicNameValuePair(“电子邮件”,E));
添加(新的BasicNameValuePair(“预防”,B));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
Log.i(“postData”,response.getStatusLine().toString());
}
捕获(例外e)
{
Log.e(“Log_标记”,“错误:+e.toString());
}  
}
};
返回null;
}
}.execute();
}
}
我已将此代码放在我的清单上:

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

你确实做错了

网络操作可能涉及不可预测的延迟。要防止这种情况 为了避免造成糟糕的用户体验,请始终执行网络操作 在UI的单独线程上。AsyncTask类提供 从UI线程启动新任务的最简单方法 有关此主题的讨论,请参阅有关多线程的博客文章 表演


bouton.setOnClickListener(新视图.OnClickListener(){
公共空间点击(新观点){
新建异步任务(){
@凌驾
受保护的Void doInBackground(Void…参数){
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://10.0.2.2:8080");
试一试{
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“nom”,N));
添加(新的BasicNameValuePair(“prenom”,P));
添加(新的BasicNameValuePair(“电子邮件”,E));
添加(新的BasicNameValuePair(“预防”,B));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
Log.i(“postData”,response.getStatusLine().toString());
}捕获(例外e){
Log.e(“Log_标记”,“错误:+e.toString());
}  
返回null;
}
}.execute();
}

}

AsyncTask
类中执行此操作。请用谷歌搜索stackoverflow上的问题。您将得到一个
NetworkMainThreadException
,因为您正在主UI线程上进行网络调用(谁会猜到呢)不是在后台。谢谢你的链接,我现在更了解踏板了,但是我在.execute()上有一个错误;我不明白我需要在()当我不放置任何内容或
null
:是的,我只是复制了一份您更正的内容,但这一行仍然有错误多个标记-语法错误,插入“)“要完成表达式-标记上的语法错误”)”,请删除此标记可能要少一些,因为他们说的是语法错误,插入“”完成表达,但不知道我能把它放在哪里
bouton.setOnClickListener(new View.OnClickListener() {
 public void onClick(View nouveau) {
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2:8080");

              try {
                  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                  nameValuePairs.add(new BasicNameValuePair("nom", N));
                  nameValuePairs.add(new BasicNameValuePair("prenom", P));
                  nameValuePairs.add(new BasicNameValuePair("email", E));
                  nameValuePairs.add(new BasicNameValuePair("prevente", B));

                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));                  
                  HttpResponse response = httpclient.execute(httppost);
                  Log.i("postData", response.getStatusLine().toString());
              } catch(Exception e) {
                       Log.e("log_tag", "Error:  "+e.toString());
              }  
            return null;
         }
     }.execute();
   }