简单的应用程序从mysql读取用户名,密码并在验证后显示下一个活动

时间:2013-09-19 08:01:21

标签: android xml-namespaces

“在android中我试图从mysql数据库读取用户名和密码,并使用'intent'显示下一个窗口”    有两个活动,主要活动和userpage.class,第一个将验证用户名和密码,使用'intent',它将调用第二个'userpage'

import package com.example.loginform;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    Button login;
    EditText username,password;
    TextView status;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    TextView tv;

    List<NameValuePair> nameValuePairs;


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

    }

    private void setup() {
        // TODO Auto-generated method stub
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        login = (Button)findViewById(R.id.login);
        status = (TextView)findViewById(R.id.tvstatus);
          tv = (TextView)findViewById(R.id.editText1);
        login.setOnClickListener(this);
    }



    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch(arg0.getId())
        {
        case R.id.login:
        login();
        break;


        }



    }

    private void login() {
        // TODO Auto-generated method stub

        try{
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("localhost/android/index.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);

            tv.setText(""+response);
            if(response.equalsIgnoreCase("User Found"))
            {

                startActivity(new Intent(this,UserPage.class));
            }
        }catch(Exception e)
        {
            e.printStackTrace();
            Toast.makeText(getBaseContext(),"Sorry error in the connection!!",Toast.LENGTH_SHORT).show();
        }
    }

}


Error log:
09-19 12:59:11.367: E/Trace(2634): error opening trace file: No such file or directory (2)

1 个答案:

答案 0 :(得分:0)

问题在于你的代码。

 httppost = new HttpPost("localhost/android/index.php");

您无法访问此链接。您必须提供IP地址和端口号以及此URL。并且您的测试设备必须位于同一网络中才能访问此URL。它会像这样

httppost = new HttpPost("http://10.0.2.2:8080/android/index.php"); 

请参阅此Connect an Android Device To a Web Service on Local Host

public class MainActivity extends Activity implements OnClickListener {
Button login;
EditText username, password;
TextView status;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
TextView tv;

List<NameValuePair> nameValuePairs;

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

}

private void setup() {
    // TODO Auto-generated method stub
    username = (EditText) findViewById(R.id.username);
    password = (EditText) findViewById(R.id.password);
    login = (Button) findViewById(R.id.login);
    status = (TextView) findViewById(R.id.tvstatus);
    tv = (TextView) findViewById(R.id.editText1);
    login.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
        case R.id.login :
        new LoginOperation().execute();
            break;

    }

}
class LoginOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        return login();
    }

    @Override
    protected void onPostExecute(String result) {
        tv.setText("" + result);
        if (result.equalsIgnoreCase("User Found")) {

            startActivity(new Intent(this, UserPage.class));
        }
    }

    @Override
    protected void onPreExecute() {
    }
}

private String login() {
    // TODO Auto-generated method stub

    try {
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("localhost/android/index.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);

        return response;
    } catch (Exception ex) {
        return "";
    }
}
 }