我正在为我的应用程序创建登录系统。它将登录信息发送到 API 并返回一个 json。
这是 LoginActivity.java
public class LoginActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Login Button Click event
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
this.retrieveLogin();
}
private void retrieveLogin() {
LoginAsyncTask task = new LoginAsyncTask();
task.execute();
}
});
// Link to Register Screen
Button btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegister);
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),RegisterActivity.class);
startActivity(i);
finish();
}
});
}
private class LoginAsyncTask extends AsyncTask<Void, Void, Void> {
Login login2;
private Activity activity;
final EditText inputEmail = (EditText) findViewById(R.id.loginEmail);
final EditText inputPassword = (EditText) findViewById(R.id.loginPassword);
final TextView loginErrorMsg = (TextView) findViewById(R.id.login_error);
String email = inputEmail.getText().toString();
String pwrd = inputPassword.getText().toString();
@Override
protected Void doInBackground(Void... params) {
//System.out.println(loginURL);
try {
//Create an HTTP client
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(loginURL);
//Perform the request and check the status code
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
try {
//Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
Gson gson = new Gson();
this.login2 = gson.fromJson(reader, Login.class);
} catch (Exception ex) {
Log.e(TAG, "Failed to parse JSON due to: " + ex);
failedLoading();
}
} else {
Log.e(TAG, "Server responded with status code: " + statusLine.getStatusCode());
failedLoading();
}
} catch(Exception ex) {
Log.e(TAG, "Failed to send HTTP POST request due to: " + ex);
failedLoading();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
@Override
protected void onPause() {
super.onPause();
//finish();
}
private void failedLoading() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, "Failed to Register. look at LogCat.", Toast.LENGTH_SHORT).show();
}
});
}
}
如果我输入无效数据进行登录,则返回以下 json:
{
"result":"KO",
"message":["Make sure you did not misspell your password."]
}
该消息已成功显示在文本视图上。
但是,当我输入有效数据进行登录时,它会返回以下 json:
{
"result":"OK",
"message":["Successfull login."],
"values":{"id":"1809","fname":"me","gender":"male"}
}
在这种情况下,我的应用程序强制关闭并获得以下 logcat:
09-30 12:55:05.445: E/AndroidRuntime(14593): FATAL EXCEPTION: main
09-30 12:55:05.445: E/AndroidRuntime(14593): java.lang.NullPointerException
09-30 12:55:05.445: E/AndroidRuntime(14593): at com.kabelash.sk.LoginActivity$LoginAsyncTask.onPostExecute(LoginActivity.java:128)
09-30 12:55:05.445: E/AndroidRuntime(14593): at com.kabelash.sk.LoginActivity$LoginAsyncTask.onPostExecute(LoginActivity.java:1)
09-30 12:55:05.445: E/AndroidRuntime(14593): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-30 12:55:05.445: E/AndroidRuntime(14593): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-30 12:55:05.445: E/AndroidRuntime(14593): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-30 12:55:05.445: E/AndroidRuntime(14593): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 12:55:05.445: E/AndroidRuntime(14593): at android.os.Looper.loop(Looper.java:137)
09-30 12:55:05.445: E/AndroidRuntime(14593): at android.app.ActivityThread.main(ActivityThread.java:4898)
09-30 12:55:05.445: E/AndroidRuntime(14593): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 12:55:05.445: E/AndroidRuntime(14593): at java.lang.reflect.Method.invoke(Method.java:511)
09-30 12:55:05.445: E/AndroidRuntime(14593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-30 12:55:05.445: E/AndroidRuntime(14593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-30 12:55:05.445: E/AndroidRuntime(14593): at dalvik.system.NativeStart.main(Native Method)
但我找不到任何解决方案。有人请帮忙。