2012-03-30 479 views
3

我的登錄系統的這一方面工作得很好,如果我有返回語句設置爲0或1,但如果我使用null失敗。這是從http://256design.com/blog/android-login-asynctask/改編而來,其中這個特定的回報看起來像我自己的代碼下面列出。ASynctask的doInBackground()中的return語句的目的是什麼?

public LoginTask(Polling activity, ProgressDialog progressDialog) 
    { 
     this.activity = activity; 
     this.progressDialog = progressDialog; 
    }  

protected Integer doInBackground(String... arg0) { 
      EditText userName = (EditText)activity.findViewById(R.id.emailEditText); 
      EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText); 


      String email = userName.getText().toString(); 
      String password = passwordEdit.getText().toString(); 
      UserFunctions userFunction = new UserFunctions(); 
      JSONObject json = userFunction.loginUser(email, password); 
      progressDialog.dismiss(); 
      // check for login response 
      //Log.v("test", Integer.toString(jsonParser.getResponseCode())); 
      try { 
       if (json.getString(KEY_SUCCESS) != null) { 
        //loginErrorMsg.setText(""); 
        //loginFragment.loginErrorMsg.setText("Success"); 
        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(activity.getApplicationContext()); 
         JSONObject json_user = json.getJSONObject("user"); 
         //Log.v("name", json_user.getString(KEY_NAME)); 
         // Clear all previous data in database 
         userFunction.logoutUser(activity.getApplicationContext()); 
         db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), 
           json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));       


         // Close Login Screen 
         //finish(); 
         //loginErrorMsg = (TextView)activity.findViewById(R.id.loginErrorMsg); 
         //loginErrorMsg.setText("logged in"); 
         //passwordEdit.setText(""); 
        }else{ 
         // Error in login 
         //progressDialog.setMessage("Incorrect username or password"); 
         //loginErrorMsg.setText("Incorrect username/password"); 
        } 

       } 

      } catch (NullPointerException e) { 
       e.printStackTrace(); 

      } 
      catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return 1; 
     } 

我用,看看responseCode教程:

protected Integer doInBackground(String... arg0) 
{ 
    String result = ""; 
    int responseCode = 0; 
    try 
    { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.256design.com/projectTransparency/project/headerLogin.php"); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("emailAddress", arg0[0])); 
      nameValuePairs.add(new BasicNameValuePair("password", arg0[1])); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     int executeCount = 0; 
     HttpResponse response; 
     do 
     { 
      progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)"); 
      // Execute HTTP Post Request 
      executeCount++; 
      response = client.execute(httppost); 
      responseCode = response.getStatusLine().getStatusCode();       
      // If you want to see the response code, you can Log it 
      // out here by calling: 
      // Log.d("256 Design", "statusCode: " + responseCode) 
     } while (executeCount < 5 && responseCode == 408); 

     BufferedReader rd = new BufferedReader(new InputStreamReader(
       response.getEntity().getContent())); 

     String line; 
     while ((line = rd.readLine()) != null) 
     { 
      result = line.trim(); 
     } 
     id = Integer.parseInt(result); 
    } 
    catch (Exception e) { 
     responseCode = 408; 
     e.printStackTrace(); 
    } 
    return responseCode; 
} 

回答

2

的目的是爲了你的工作結果(這是一個工作線程執行)傳遞給onPostExecute,以便處理在UI線程上的結果。如果您想更新用戶界面以響應成功的作業運行,則這是必需的。

+1

失敗狀態,同時在這個線程每個響應說幾乎完全一樣的東西,你到底在兩句話,並與第二句是把銀子我的實際目標是我今晚一直未能實現全部目標(在提出這個問題時,我並沒有意識到這一點,這就是爲什麼我從doinbackground更新textview時遇到了很多麻煩 - 事實上我在onPostExecute中執行。完美,謝謝! – Davek804 2012-03-30 08:14:21

2

DoInBackground返回值postExecute方法,並傳遞null不驗證條件:

if(headerCode == 202) 
activity.login(id); 
1
protected class InitTask extends AsyncTask<Context, Integer, Integer> 

在上面的代碼行中,我們定義了我們的子類以及將傳遞給回調函數的三個參數。回調看起來像這樣:

doInBackground()

@Override 
protected Integer doInBackground(Context... params) { 
    return super.doInBackground(params) 
} 

任何在該方法中處理的sperate線程處理。 請注意,返回值的數據類型是一個Integer,並對應於類定義中的第三個參數類型。當此線程完成時,從此方法返回的此值將傳遞給onPostExecute()方法。

如果您在返回通過null,則它的方法onPostExecuted()

相關問題