使用AsyncTask

道歉,如果这是一个简单的问题,但我很新,并仍在学习。

我有一个应用程序,当我的用户点击buttonlogin后,input他们的详细信息,这是崩溃与android.os.NetworkOnMainThreadException我已经发现这是因为我在主线程上执行networking操作,并解决我需要使用AsyncTask,但我坚持使用语法。

这是我的代码点击button后,调用一个函数来连接,然后parsingjson到sqlite数据库。

// 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() 

如果在doInBackground()之前或期间没有使用它们显示Dialog ,进度或其他任务,则可能不需要onProgressUpdate()doInBackground()

onPreExecute()可用于初始化并显示ProgressDialog 。 那么可以在onPostExecute()

任务完成后

onPostExecute()将被调用。 如果这个类是你的Activity的内部类,那么你可以在那里更新UI元素,或者在任务完成后调用一个函数来运行代码。 如果它是一个单独的文件比Activity那么你可以使用一个interface并创build一个callBack Activity并在任务完成后运行代码。

如果需要的话,这个答案将讨论使用一个callback接口

确保除doInBackground()之外的任何方法都进行UI更新,或者在除doInBackground()之外的任何函数中发送回Activity 。 networking操作等繁重工作应该在doInBackground()

此外,请务必完整阅读AsyncTask文档 。 尤其是线程规则