如果有两个错误:
Error parsing data org.json.JSONException: End of input at character 3 of & FATAL EXCEPTION: AsyncTask #1
和:
Activity rng.events.LoginActivity has leaked window at rng.events.LoginActivity$LogInUser.onPreExecute(LoginActivity.java:84)
问题是,注册后它会自动让您登录,一旦您注销并强制关闭应用程序,然后再次尝试登录,我就会收到这些错误。
LoginActivity 的代码如下。
package rng.events;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import JSONnDB.DatabaseHandler;
import JSONnDB.UserFunctions;
public class LoginActivity extends Activity {
Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_F_NAME = "f_name";
private static String KEY_L_NAME = "l_name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
private ProgressDialog pDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new LogInUser().execute();
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
}
/**
* Background Async Task to Create new product
* */
class LogInUser extends AsyncTask<String, String, String> {
protected void onPreExecute() {
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Logging in..Please Wait..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
super.onPreExecute();
}
protected String doInBackground(String... args) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
//loginErrorMsg.setText("");
int res = json.getInt(KEY_SUCCESS);
if(res == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_F_NAME), json_user.getString(KEY_L_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
//loginErrorMsg.setText("Incorrect username/password");
Log.i("Log in: ", "Error occured while loggin in");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
日志输出
06-11 13:57:08.139: D/dalvikvm(794): GC_FOR_ALLOC freed 68K, 4% free 6528K/6787K, paused 28ms
06-11 13:57:08.149: I/dalvikvm-heap(794): Grow heap (frag case) to 6.861MB for 448916-byte allocation
06-11 13:57:08.199: D/dalvikvm(794): GC_CONCURRENT freed 3K, 4% free 6963K/7239K, paused 2ms+2ms
06-11 13:57:08.269: D/dalvikvm(794): GC_FOR_ALLOC freed 444K, 10% free 6747K/7495K, paused 19ms
06-11 13:57:08.279: I/dalvikvm-heap(794): Grow heap (frag case) to 7.647MB for 1048592-byte allocation
06-11 13:57:08.319: D/dalvikvm(794): GC_CONCURRENT freed <1K, 10% free 7771K/8583K, paused 2ms+2ms
06-11 13:57:08.339: D/dalvikvm(794): GC_FOR_ALLOC freed 0K, 10% free 7771K/8583K, paused 15ms
06-11 13:57:08.339: I/dalvikvm-heap(794): Grow heap (frag case) to 8.090MB for 465140-byte allocation
06-11 13:57:08.359: D/dalvikvm(794): GC_FOR_ALLOC freed 0K, 10% free 8225K/9095K, paused 14ms
06-11 13:57:08.429: D/libEGL(794): loaded /system/lib/egl/libGLES_android.so
06-11 13:57:08.439: D/libEGL(794): loaded /system/lib/egl/libEGL_mali.so
06-11 13:57:08.449: D/libEGL(794): loaded /system/lib/egl/libGLESv1_CM_mali.so
06-11 13:57:08.449: D/libEGL(794): loaded /system/lib/egl/libGLESv2_mali.so
06-11 13:57:08.469: D/OpenGLRenderer(794): Enabling debug mode 0
06-11 13:57:17.339: D/Button(794): Login
06-11 13:57:17.659: E/JSON(794):
06-11 13:57:17.659: E/JSON Parser(794): Error parsing data org.json.JSONException: End of input at character 3 of
06-11 13:57:17.669: W/dalvikvm(794): threadid=12: thread exiting with uncaught exception (group=0x409d91f8)
06-11 13:57:17.669: E/AndroidRuntime(794): FATAL EXCEPTION: AsyncTask #1
06-11 13:57:17.669: E/AndroidRuntime(794): java.lang.RuntimeException: An error occured while executing doInBackground()
06-11 13:57:17.669: E/AndroidRuntime(794): at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-11 13:57:17.669: E/AndroidRuntime(794): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.lang.Thread.run(Thread.java:856)
06-11 13:57:17.669: E/AndroidRuntime(794): Caused by: java.lang.NullPointerException
06-11 13:57:17.669: E/AndroidRuntime(794): at rng.events.LoginActivity$LogInUser.doInBackground(LoginActivity.java:95)
06-11 13:57:17.669: E/AndroidRuntime(794): at rng.events.LoginActivity$LogInUser.doInBackground(LoginActivity.java:1)
06-11 13:57:17.669: E/AndroidRuntime(794): at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-11 13:57:17.669: E/AndroidRuntime(794): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-11 13:57:17.669: E/AndroidRuntime(794): ... 5 more
06-11 13:57:17.939: D/OpenGLRenderer(794): Flushing caches (mode 0)
06-11 13:57:17.959: D/OpenGLRenderer(794): Flushing caches (mode 0)
06-11 13:57:18.259: D/OpenGLRenderer(794): Flushing caches (mode 1)
06-11 13:57:18.269: E/WindowManager(794): Activity rng.events.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4106d338 that was originally added here
06-11 13:57:18.269: E/WindowManager(794): android.view.WindowLeaked: Activity rng.events.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4106d338 that was originally added here
06-11 13:57:18.269: E/WindowManager(794): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
06-11 13:57:18.269: E/WindowManager(794): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
06-11 13:57:18.269: E/WindowManager(794): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-11 13:57:18.269: E/WindowManager(794): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-11 13:57:18.269: E/WindowManager(794): at android.view.Window$LocalWindowManager.addView(Window.java:537)
06-11 13:57:18.269: E/WindowManager(794): at android.app.Dialog.show(Dialog.java:285)
06-11 13:57:18.269: E/WindowManager(794): at rng.events.LoginActivity$LogInUser.onPreExecute(LoginActivity.java:82)
06-11 13:57:18.269: E/WindowManager(794): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-11 13:57:18.269: E/WindowManager(794): at android.os.AsyncTask.execute(AsyncTask.java:511)
06-11 13:57:18.269: E/WindowManager(794): at rng.events.LoginActivity$1.onClick(LoginActivity.java:58)
06-11 13:57:18.269: E/WindowManager(794): at android.view.View.performClick(View.java:3511)
06-11 13:57:18.269: E/WindowManager(794): at android.view.View$PerformClick.run(View.java:14109)
06-11 13:57:18.269: E/WindowManager(794): at android.os.Handler.handleCallback(Handler.java:605)
06-11 13:57:18.269: E/WindowManager(794): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 13:57:18.269: E/WindowManager(794): at android.os.Looper.loop(Looper.java:137)
06-11 13:57:18.269: E/WindowManager(794): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-11 13:57:18.269: E/WindowManager(794): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:57:18.269: E/WindowManager(794): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 13:57:18.269: E/WindowManager(794): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-11 13:57:18.269: E/WindowManager(794): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-11 13:57:18.269: E/WindowManager(794): at dalvik.system.NativeStart.main(Native Method)