我正在尝试创建简单的登录表单。如果验证了用户凭据,则以下 URL 返回“true”,否则返回“false”。返回简单字符串。
http://......../login.php?user=rashid&pass=rashid
当我按下登录按钮时程序崩溃。这在 LogCat 上显示:-
致命异常:main - com.example.logintry.MainActivity$VerifyLogin.onPostExecution 处的 java.lang.NullPointerException
这是activit_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();
}
}
}
}