简体   繁体   English

Android PHP用户注册

[英]Android PHP user registration

I am new to android, How can i insert values from android to php mysql i tried the following code it does not showing any error but not working. 我是android的新手,如何将android的值插入php mysql,我尝试了以下代码,但未显示任何错误,但无法正常工作。 can anyone help me. 谁能帮我。 Can anyone correct my mistake. 谁能纠正我的错误。

public class MainActivity extends Activity{

    EditText username, email, password;
    Button btn;
    HttpResponse response;

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = (EditText) findViewById(R.id.usr_name);
        email = (EditText) findViewById(R.id.email);
        password = (EditText) findViewById(R.id.password);

        btn = (Button) findViewById(R.id.regbtn);
        btn.setOnClickListener(new View.OnClickListener(){


            public void onClick(View v){

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest.php");

                try{

                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("uname", username.toString()));
                    nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    response = httpclient.execute(httppost);

                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

}

I tested your example on my local VM and for me there was "06-01 00:22:17.671: W/System.err(2024): android.os.NetworkOnMainThreadException" as I commented before. 我在本地VM上测试了您的示例,对我来说,我之前曾评论过“ 06-01 00:22:17.671:W / System.err(2024):android.os.NetworkOnMainThreadException”

Dont do networking on the main thread. 不要在主线程上进行网络连接。

An (other) example on how to use async tasks is postet here . 关于如何使用异步任务的(其他的)例子postet 这里

Async Tasks are executed in parallel to the main thread, so they are NOT blocking the main thread. 异步任务是与主线程并行执行的,因此它们不会阻塞主线程。 Blocking the main thread is bad because it for example prevents the user interface from beeing redrawn / the interface cant handle input. 阻塞主线程是不好的,因为例如它阻止用户界面重绘/界面无法处理输入。
Therfore android throws an NetworkOnMainThreadException to prevent you from doing networking (a time consuming tasks) on the main thread. 因此,android会引发NetworkOnMainThreadException来防止您在主线程上进行网络连接(这是一项耗时的任务)。

An example implementation can be found here . 可以在此处找到示例实现。

Its probably not working because you are trying to send your message to php on the mainUI thread. 它可能无法正常工作,因为您正在尝试将消息发送到mainUI线程上的php。 Try putting your code into an asynctask like this 尝试将代码放入这样的asynctask中

class Task extends AsyncTask<Void, Void, Void>{

    @Override
    protected void doInBackground(Void... params){


    HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/androidtest.php");

            try{

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("uname", username.toString()));
                nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response = httpclient.execute(httppost);

            }catch(Exception e){
                e.printStackTrace();
            }
    }
}

And then call it like this 然后这样称呼它

new Task().execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM