如果这是一个简单的问题,我深表歉意,但我对此很陌生,仍在学习中。
我有一个应用程序,当我的用户在输入他们的详细信息后单击按钮登录时,它因 android.os.NetworkOnMainThreadException 而崩溃 我发现这是因为我在主线程上执行网络操作并解决我需要使用 AsyncTask,但是我被语法困住了。
这是我点击按钮后的代码,调用一个函数进行连接,然后将 json 解析为 sqlite db。
// 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);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
如何将其更改为正确的类?我没有传递 URL 等。我认为它需要像这样,但我真的很难使语法正确。
非常感谢!!!
class login extends AsyncTask<Void, Void, Void> {
private Exception exception;
protected ??? doInBackground(???) {
try {
最佳答案
应该是这样的
public class TalkToServer extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
/*
* do things before doInBackground() code runs
* such as preparing and showing a Dialog or ProgressBar
*/
}
@Override
protected void onProgressUpdate(Void... values) {
/*
* updating data
* such a Dialog or ProgressBar
*/
}
@Override
protected Void doInBackground(Void... params) {
//do your work here
return null;
}
@Override
protected void onPostExecute(Void result) {
/*
* do something with data here
* display it or send to mainactivity
* close any dialogs/ProgressBars/etc...
*/
}
}
然后你可以执行它
TalkToServer myTask = new MyTask(); // can add params for a constructor if needed
myTask.execute(); // here is where you would pass data to doInBackground()
如果您不使用 onProgressUpdate()
或 onPreExecute()
来显示 Dialog
、进度或doInBackground()
之前或期间的其他任务。
onPreExecute() 可用于初始化和显示 ProgressDialog
。然后可以在 onPostExecute()
任务完成后
onPostExecute()
将被调用。如果该类是您的 Activity
的内部类,那么您可以在那里更新 UI
元素,或者在任务完成后调用一个函数来运行代码。如果它是一个与 Activity
不同的文件,那么您可以使用 interface
并创建一个 callBack
到 Activity
并在任务完成后在那里运行代码。
This answer talks about using an interface for a callback if you need
确保任何 UI
更新是在 doInBackground()
之外的任何方法中完成的,或者在 之外的任何函数中发送回
。网络操作等繁重工作应在 Activity
>doInBackground()doInBackground()
中完成。
此外,请务必通读 AsyncTask Docs完全地。尤其是线程规则
关于android - 使用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18898039/