我正在尝试在Android 4.0设备上进行登录和注册。所以我不得不更新我的代码以使用Asynctask,我收到错误,不知道如何解决它们。任何帮助将不胜感激!
public class RegisterActivity extends Activity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;
//JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
inputEmail = (EditText) findViewById(R.id.registerEmail);
inputPassword = (EditText) findViewById(R.id.registerPassword);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new RegisterTask().execute();
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
// Close Registration View
finish();
}
});
}
class RegisterTask extends AsyncTask<String, String, String> {
int success;
String email;
String password;
String name;
@Override
protected void onPreExecute() {
super.onPreExecute();
email = inputEmail.getText().toString();
password = inputPassword.getText().toString();
name = inputFullName.getText().toString();
}
@Override
protected String doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(name, email, password);
Log.d("Create Response", json.toString());
try {
int success = json.getInt(KEY_SUCCESS);
if (success == 1) {
// DATABASE HANDLER
// 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_NAME),
json_user.getString(KEY_EMAIL),
json_user.getString(KEY_UID),
json_user.getString(KEY_CREATED_AT));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
if (success == 1) {
Intent dashboard = new Intent(getApplicationContext(), LoginActivity.class);
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
finish();
}else{
//tvLError.setText("Incorrect username/password");
}
}
}
}
我得到的错误日志......
12-02 20:45:23.140:E / JSON Parser(28208):解析数据时出错org.json.JSONException:Value
<br of type java.lang.String cannot be converted to JSONObject
12-02 20:45:23.140: W/dalvikvm(28208): threadid=13: thread exiting with uncaught exception (group=0x415e62a0)
12-02 20:45:23.140: E/AndroidRuntime(28208): FATAL EXCEPTION: AsyncTask #1
12-02 20:45:23.140: E/AndroidRuntime(28208): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.lang.Thread.run(Thread.java:856)
12-02 20:45:23.140: E/AndroidRuntime(28208): Caused by: java.lang.NullPointerException
12-02 20:45:23.140: E/AndroidRuntime(28208): at com.example.cs309.gotoclass.RegisterActivity$RegisterTask.doInBackground(RegisterActivity.java:89)
12-02 20:45:23.140: E/AndroidRuntime(28208): at com.example.cs309.gotoclass.RegisterActivity$RegisterTask.doInBackground(RegisterActivity.java:1)
12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 20:45:23.140: E/AndroidRuntime(28208): ... 5 more
registerUser
public JSONObject registerUser(String name, String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
答案 0 :(得分:0)
确保验证您的空值的登录详细信息&amp;然后只调用AsychTask&amp;调用registerUser()方法。
答案 1 :(得分:0)
如果没有prper源代码,很难搞清楚。但请检查RegisterActivity.java中的第89行,它是从doInBackground()调用的。设置一个断点并检查哪个项目为NULL(因为你得到了NullPointerException)。一旦你解决了它就可以了。