Android 使用异步任务的用户身份验证

Android 使用异步任务的用户身份验证,android,eclipse,android-asynctask,Android,Eclipse,Android Asynctask,我正在尝试创建简单的登录表单。如果验证了用户凭据,则以下URL返回“true”,否则返回“false”。返回简单字符串 http://login.php?user=rashid&pass=rashid 当我按下登录按钮时,程序崩溃。这在LogCat上显示:- 致命异常:main-java.lang.NullPointerException位于 com.example.logintry.main活动$VerifyLogin.onPostExecution 这里是activit\u main.xml

我正在尝试创建简单的登录表单。如果验证了用户凭据,则以下URL返回“true”,否则返回“false”。返回简单字符串

http://login.php?user=rashid&pass=rashid

当我按下登录按钮时,程序崩溃。这在LogCat上显示:-

致命异常:main-java.lang.NullPointerException位于 com.example.logintry.main活动$VerifyLogin.onPostExecution

这里是activit\u main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"    
android:padding="10dp"
 >

<EditText
    android:id="@+id/etUser"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="User Name"
    android:text="rashid"
    android:ems="10" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Password"
    android:text="rashid123"
    android:inputType="textPassword" />

<Button
    android:id="@+id/bLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Login" />

</LinearLayout>

这里是MainActivity.java 出于安全原因,我没有添加实际的IP

package com.example.logintry;

import java.io.IOException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

private EditText etUser, etPassword;
private Button bLogin;
private String u, p;
private final String KEY_LOGIN_URL = "http://.............../login.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etUser = (EditText) findViewById(R.id.etUser);
    etPassword = (EditText) findViewById(R.id.etPassword);
    bLogin = (Button) findViewById(R.id.bLogin);
    bLogin.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    u = etUser.getText().toString();
    p = etPassword.getText().toString();
    VerifyLogin runner = new VerifyLogin();
    runner.execute();

}

private class VerifyLogin extends AsyncTask<URL, Void, Void> {
    String msg = "";
    HttpClient httpClient;
    HttpGet httpGet;
    HttpResponse response;
    HttpEntity entity;

    @Override
    protected Void doInBackground(URL... urls) {
        // TODO Auto-generated method stub
        try {
            httpClient = new DefaultHttpClient();
            httpGet = new HttpGet(KEY_LOGIN_URL + "?user=" + u + "&pass=" + p);
            response = httpClient.execute(httpGet);
            entity = response.getEntity();
            msg = EntityUtils.toString(entity);

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

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        if (result.equals("true")) {
            Toast.makeText(getApplicationContext(), "Valid password",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Invalid Password",
                    Toast.LENGTH_LONG).show();
        }

    }

}

}
package com.example.logintry;
导入java.io.IOException;
导入java.net.URL;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MainActivity扩展活动实现View.OnClickListener{
私有编辑文本etUser,etPassword;
私人按钮博客;
私有字符串u,p;
私有最终字符串密钥\u登录\u URL=”http://.............../login.php";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUser=(EditText)findViewById(R.id.etUser);
etPassword=(EditText)findViewById(R.id.etPassword);
bLogin=(按钮)findviewbyd(R.id.bLogin);
setOnClickListener(this);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
u=etUser.getText().toString();
p=etPassword.getText().toString();
VerifyLogin runner=新建VerifyLogin();
runner.execute();
}
私有类VerifyLogin扩展异步任务{
字符串msg=“”;
HttpClient-HttpClient;
HttpGet-HttpGet;
HttpResponse响应;
http实体;
@凌驾
受保护的Void doInBackground(URL…URL){
//TODO自动生成的方法存根
试一试{
httpClient=新的DefaultHttpClient();
httpGet=newhttpget(KEY\u LOGIN\u URL+“?user=“+u+”&pass=“+p”);
response=httpClient.execute(httpGet);
entity=response.getEntity();
msg=EntityUtils.toString(实体);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
if(result.equals(“true”)){
Toast.makeText(getApplicationContext(),“有效密码”,
Toast.LENGTH_LONG).show();
}否则{
Toast.makeText(getApplicationContext(),“无效密码”,
Toast.LENGTH_LONG).show();
}
}
}
}

结果的类型为
Void
,该类型始终为空,因此
result.equals(“true”)
将抛出NPE


您需要将VerifyLogin类更改为
扩展AsyncTask
,并使doInBackground按照您的执行后方法的预期返回“true”或“false”。

问题在哪里?你有什么问题?有什么问题吗?很抱歉告诉你。实际上,当我按下登录按钮时,程序崩溃了。这显示在LogCat:---致命异常:com.example.logintry.main活动$VerifyLogin.onpostExecution谢谢。使用extends AsyncTaskAh解决了问题是的,你是对的。在我的回答中,我已经纠正了这一点(我似乎总是在AsyncTask上把一般顺序弄错了——叹气)。