使用AsyncTask我有一个应用程序,当我的用户在输入他们的详细信息后点击这个按钮登录时,它会与android.os.NetworkOnMainThreadException崩溃-我发现这是因为我在主线程上执行一个网络操作,并且为了解决我需要使用AsyncTask的问题,我被语法卡住了。以下是单击按钮后的代码,调用函数连接,然后将json解析为sqlitdb。// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(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_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);
}
}
});我如何将它更改为正确的类?我没有传递URL等等,我认为应该是这样的,但我真的很难把语法弄对。
- 3 回答
- 0 关注
- 219 浏览
添加回答
举报
0/150
提交
取消