使用php和mysql在android中登录表单 [英] login form in android using php and mysql

查看:57
本文介绍了使用php和mysql在android中登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在android中使用php和mysql开发登录表单每次我尝试在模拟器中测试它会显示以下错误

在堆栈跟踪中



02-02 17:20:05.137 1145-1145 / green.gemstouch.com.samplelogin E / AndroidRuntime:致命异常:主要
流程:green.gemstouch.com.samplelogin,PID:1145

android.os.NetworkOnMainThreadException



查看代码下面

Hi
I am developing a login form in android using php and mysql each time i try to test in the emulator it shows the following error
in the stack trace

02-02 17:20:05.137 1145-1145/green.gemstouch.com.samplelogin E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: green.gemstouch.com.samplelogin, PID: 1145
android.os.NetworkOnMainThreadException

see the code below

public class MyActivity extends ActionBarActivity  {

    EditText username, password;
    Button login;
    TextView status;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<namevaluepair> nameValuePairs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        
        setup();
    }

    private void setup() {
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        status = (TextView) findViewById(R.id.status);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                switch (arg0.getId()){
                    case R.id.login:

                        login();    
                        break;
                }
            }
        });
    }

    private void login() {
        try{
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2/akadalogin/sample.php");
            nameValuePairs = new ArrayList<namevaluepair>(1);
            nameValuePairs.add(new BasicNameValuePair("username", username.getText().toString().trim()));
            nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString().trim()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);

            ResponseHandler<string> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);

            status.setText(""+response);
            if (response.equalsIgnoreCase("User found")){
                startActivity(new Intent(MyActivity.this, UserPage.class));
            }



        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



请帮助


Kindly help

推荐答案

您需要知道几个问题。



1.你不能从主线程做任何与网络相关的工作



这样做你将不得不在单独的线程中调用网络相关的工作。

最简单的方法是:使用AsyncTask类。



您还尝试使用用户帐户登录。在这种情况下,还要看一下Apache提供的HttpContex。 HttpContext将召回您的cookie
Few issue you need to know.

1. You cannot do any network related work from the main thread

to do so you will have to call network related work in separate thread.
The simplest way would be: using AsyncTask class.

And also you are trying to log in with user account. In this case also take a look at HttpContex, provided by Apache. HttpContext will recall your cookies


这篇关于使用php和mysql在android中登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆