1

此活动用于登录。它通过“GET”将用户名和密码发布到 PHP 验证文件,这将得到响应(成功或失败)。但它只能显示“错误发生”(既不成功也不失败)这里是代码:

public class Login extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    findViewsById();
    setListeners();
}
private Button btn_login;
private EditText edt_username;
private EditText edt_password;

private void findViewsById() {
    edt_username = (EditText) findViewById(R.id.edt_username);
    edt_password = (EditText) findViewById(R.id.edt_password);
    btn_login = (Button) findViewById(R.id.btn_login);              
}


private void setListeners(){
    btn_login.setOnClickListener(login);
}
private Button.OnClickListener login = new Button. OnClickListener() {

public void onClick(View v) {
   new startLogin().execute();      
}



};

private class startLogin extends AsyncTask <Void, Void, Void> {

    String result;
    String url = "http://www.abc.com/verify.php";
    String fail = "FAILED";
    String error = "error";
    final ProgressDialog progDlg = new ProgressDialog(Login.this);

    String username = edt_username.getText().toString();
    String password = edt_password.getText().toString();


    protected void onPreExecute() {
        super.onPreExecute();

        progDlg.setTitle("Please wait");
        progDlg.setMessage("Loading...");
        progDlg.setIcon(android.R.drawable.ic_dialog_info);
        progDlg.setCancelable(false);
        progDlg.show();


        //add username and password to the site

        url += "?username=" + username;
        url += "&password="  + password;    

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        HttpResponse httpResponse = null;
        try {



            HttpGet httpGet = new HttpGet (url) ;
            httpResponse = new DefaultHttpClient().execute (httpGet);

            if(httpResponse.getStatusLine().getStatusCode() == 200){      
                    result = EntityUtils.toString(httpResponse.getEntity());     
                }    
                else{                           
                    //connection error    
                    result = error;
                }    


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

           } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }catch (Exception e){
                        e.printStackTrace();
                        }

        return null;
    }

    protected void onPostExecute(Void unused){

        if (result == fail) {
            progDlg.cancel();
            Toast.makeText(Login.this, R.string.fail, Toast.LENGTH_SHORT).show();  //R.string.fail = "username or password is wrong"
        } else if (result == error) {
            progDlg.cancel();
            Toast.makeText(Login.this, R.string.error, Toast.LENGTH_SHORT).show(); //R.string.error = "there is a connection error"
        }
        else if (result == "SUCCESS"){
            progDlg.cancel();
            Intent it = new Intent ();
            it.setClass (Login.this, Main.class);
            startActivity (it);

        } else{
            progDlg.cancel();
            Toast.makeText(Login.this, "error occure", Toast.LENGTH_SHORT).show();
        }

    }


}}

这是验证文件:

<?php
$username = $_GET["username"];
$password = $_GET["password"];
$ourFileName = "users/" . $username . ".txt";
    if (file_exists($ourFileName))
     {
    $fh = fopen($ourFileName, 'r');
$theData = fread($fh, 1024);
fclose($fh);

if ($password == $theData)
{
    echo SUCCESS;
}  else
{
    echo FAILED;
}
    }else  {
            echo FAILED;}
     ?>

日志猫

07-05 13:10:57.065: W/ActivityThread(274): Application com.android.safe is waiting for the debugger on port 8100...
07-05 13:10:57.085: I/System.out(274): Sending WAIT chunk
07-05 13:10:57.155: I/dalvikvm(274): Debugger is active
07-05 13:10:57.285: I/System.out(274): Debugger has connected
07-05 13:10:57.285: I/System.out(274): waiting for debugger to settle...
07-05 13:10:57.486: I/System.out(274): waiting for debugger to settle...
07-05 13:10:57.685: I/System.out(274): waiting for debugger to settle...
07-05 13:10:57.885: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.095: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.295: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.495: I/System.out(274): waiting for debugger to settle...
07-05 13:10:58.695: I/System.out(274): debugger has settled (1326)
07-05 13:10:58.985: D/dalvikvm(274): GC_EXTERNAL_ALLOC freed 805 objects / 58072 bytes in 43ms
07-05 13:10:59.255: D/dalvikvm(274): GC_EXTERNAL_ALLOC freed 222 objects / 10616 bytes in 42ms
07-05 13:11:45.615: W/KeyCharacterMap(274): No keyboard for id 0
07-05 13:11:45.615: W/KeyCharacterMap(274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-05 13:12:05.345: D/dalvikvm(274): GC_FOR_MALLOC freed 6458 objects / 265088 bytes in 138ms
07-05 13:12:08.036: W/System.err(274): java.lang.IllegalArgumentException: Illegal character in query at index 52: http://panel.o-safe.net/vb_verify.php?username=guest &password=guest
07-05 13:12:08.046: W/System.err(274):  at java.net.URI.create(URI.java:970)
07-05 13:12:08.046: W/System.err(274):  at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
07-05 13:12:08.046: W/System.err(274):  at com.android.safe.Login$startLogin.doInBackground(Login.java:100)
07-05 13:12:08.056: W/System.err(274):  at com.android.safe.Login$startLogin.doInBackground(Login.java:1)
07-05 13:12:08.056: W/System.err(274):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-05 13:12:08.056: W/System.err(274):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-05 13:12:08.066: W/System.err(274):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-05 13:12:08.066: W/System.err(274):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
07-05 13:12:08.066: W/System.err(274):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
07-05 13:12:08.066: W/System.err(274):  at java.lang.Thread.run(Thread.java:1096)
07-05 13:12:08.076: D/AndroidRuntime(274): Shutting down VM
07-05 13:12:08.076: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-05 13:12:08.116: E/AndroidRuntime(274): FATAL EXCEPTION: main
07-05 13:12:08.116: E/AndroidRuntime(274): java.lang.NullPointerException
07-05 13:12:08.116: E/AndroidRuntime(274):  at com.android.safe.Login$startLogin.onPostExecute(Login.java:127)
07-05 13:12:08.116: E/AndroidRuntime(274):  at com.android.safe.Login$startLogin.onPostExecute(Login.java:1)
07-05 13:12:08.116: E/AndroidRuntime(274):  at android.os.AsyncTask.finish(AsyncTask.java:417)
07-05 13:12:08.116: E/AndroidRuntime(274):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-05 13:12:08.116: E/AndroidRuntime(274):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-05 13:12:08.116: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 13:12:08.116: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)
07-05 13:12:08.116: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 13:12:08.116: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 13:12:08.116: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 13:12:08.116: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 13:12:08.116: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 13:12:08.116: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

请通过在浏览器上检查 PHP 脚本来确保它工作正常

我注意到您的代码中有以下错误

  • ==您使用而不是equals“字符串”类中的方法比较两个字符串。因此,将所有== result比较更改为equalsinonPostExecute方法。

更新代码

protected void onPostExecute(Void unused){

    if (result.equals(fail)) {
        progDlg.cancel();
        Toast.makeText(Login.this, R.string.fail, Toast.LENGTH_SHORT).show();  //R.string.fail = "username or password is wrong"
    } else if (result.equals(error)) {
        progDlg.cancel();
        Toast.makeText(Login.this, R.string.error, Toast.LENGTH_SHORT).show(); //R.string.error = "there is a connection error"
    }
    else if (result.equals("SUCCESS")){
        progDlg.cancel();
        Intent it = new Intent ();
        it.setClass (Login.this, Main.class);
        startActivity (it);

    } else{
        progDlg.cancel();
        Toast.makeText(Login.this, "error occure", Toast.LENGTH_SHORT).show();
    }

}
  • 另一件事。您可以将值从“doInBackGround”方法返回到onPostExecute方法。我的意思是,您不必总是对全局变量进行更改。这里变量result。为此,您需要AsyncTask在扩展签名时更改签名。

所以改变

private class startLogin extends AsyncTask <Void, Void, Void>

private class startLogin extends AsyncTask <Void, Void, String>

第三个参数是Result类型。现在您必须更改以下方法以适应上述更改

protected Void doInBackground(Void... arg0) {

protected String doInBackground(Void... arg0) {

protected void onPostExecute(Void unused){

protected void onPostExecute(String result){

这将帮助您直接从doInBackGround方法返回状态字符串onPostExecute

于 2012-07-05T12:08:40.427 回答