2012-03-30 2 views
3

returnステートメントを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を見てみましょう:postExecute方法、および通過nullに

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

目的は、UIスレッド上の結果を処理するために、onPostExecuteに(ワーカースレッドで実行される)あなたの仕事の結果を渡すことです。これは、正常に実行されたジョブの実行に応答してユーザーインターフェイスを更新する場合に必要です。

+1

上の条件を失敗したが、あなたは正確に2通の文で第二の文があることでそれをレイアウト私の実際の目標は、私が今晩一晩中達成することに失敗していたことです。質問をしたときに、私がdoinbackgroundからテキストビューを更新するのに苦労していた理由がわかりませんでした - 私はonPostExecuteでそれを行います。完全に、ありがとう! – Davek804

2

DoInBackgroundの戻り値は、条件を検証しません:

if(headerCode == 202) 
activity.login(id); 
1

はいそのpostExecuteに送信する値:

http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result

保護のボイドonPostExecute(結果結果) doInBackground(PARAMS ...)後のUIスレッド上

実行します。

指定された結果は、doInBackground(Params ...)による の値です。

タスクがキャンセルされた場合、このメソッドは呼び出されません。

パラメータ: 結果doInBackground(Params ...)によって計算された演算の結果。

1
protected class InitTask extends AsyncTask<Context, Integer, Integer> 

上記の行では、コールバックに渡されるサブクラスと3つのパラメータを定義しています。コールバックは次のようになります。

doInBackground()

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

この方法で処理されたものはすべてsperateスレッドで処理されます。 戻り値のデータ型はIntegerで、クラス定義の3番目の型パラメータに対応しています。このメソッドが返すこの値は、このスレッドがを完了したときにonPostExecute()メソッドに渡されます。

そして、あなたはリターンでヌルを渡した場合、このスレッド内の各応答はほぼ正確に同じことを言っている間、それは方法onPostExecuted()

関連する問題