2014-05-19 4 views
1

나는 AsyncTask을 실행하기 위해 노력하고 있지만, 때 시작하고 doInBackground 마무리 (값이 반환)가 OnPostExecute를 생략하고 내가 OnPostExecute의 값을 변경하기 전에, 그래서 난 'if condition를 실행하려고, 아래의 코드 requestTask2.execute()를 실행 AsyncTask 널 가져 왔어.Android에서 OnPostExecute가 끝날 때까지 기다리는 방법은 무엇입니까?

나 코드로 설명하자 내가 if 조건 작품 전에 OnPostExecute 기다릴 수있는 방법

public void onClick(DialogInterface dialog,int id) { 

        Intent gt = new Intent(MainActivity.this, favorite.class); 
        String password = userInput.getText().toString(); 
        String kadi = userInput2.getText().toString(); 
RequestTask2 requestTask2 = new RequestTask2(); 
requestTask2.execute("http://www.example.com/androfav/?fav2="+kadi+":"+password).get(); 


if (asd2[0][0]!=null && asd2[1][0]!=null) { 
// This if condition works before on Post Excecute and it is causing the problem. 


if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi)) { 
// Codes 
}} 

class RequestTask2 extends AsyncTask<String, String, String> { 
    private ProgressDialog dialog = new ProgressDialog(MainActivity.this); 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 

     super.onPreExecute(); 
     dialog.setMessage("Diziler Yükleniyor \n Lütfen Bekleyin..."); 
     dialog.show(); 
     dialog.setCancelable(false); 
    } 

    @Override 
    protected String doInBackground(String... uri2) { 
     HttpClient httpclient2 = new DefaultHttpClient(); 
     HttpResponse response2; 
     String responseString2 = null; 
     try { 
      response2 = httpclient2.execute(new HttpGet(uri2[0])); 
      StatusLine statusLine2 = response2.getStatusLine(); 
      if (statusLine2.getStatusCode() == HttpStatus.SC_OK) { 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response2.getEntity().writeTo(out); 
       out.close(); 
       responseString2 = out.toString(); 
      } else { 
       // Closes the connection. 
       response2.getEntity().getContent().close(); 
       throw new IOException(statusLine2.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 
      // TODO Handle problems.. 
     } catch (IOException e) { 
      // TODO Handle problems.. 
     } 
     return responseString2; 
    } 
    @Override 
    protected void onPostExecute(String result2) { 
     super.onPostExecute(result2); 
     try { 

      JSONArray jsonResponse2 = new JSONArray(result2); 
      asd2 = new String[3][jsonResponse2.length()]; 
    //............................... Codes 
     dialog.dismiss(); 
    } 

} 

합니다. 희망은 나 자신을 이해할 수 있습니다. 미리 감사드립니다.

답변

1

AsyncTask는 비동기입니다. if 조건을 onPostExecute으로 옮겨야합니다.

onPostExecute

JSONArray jsonResponse2 = new JSONArray(result2); 
asd2 = new String[3][jsonResponse2.length()]; 
if (asd2[0][0]!=null && asd2[1][0]!=null) { 


if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi)) { 
// Codes 
} 
} 

편집에 아래로 이동

: 나는 당신이 get()라고 알 din't

. get()을 호출하면 Asynctask가 더 이상 비동기가되지 않습니다. get() 전화는 절대로 execute()이면 안됩니다.

왜 작업이 끝나기를 기다리는 UI 스레드를 차단하는 get()을 호출해야합니까?

+0

답장을 보내 주셔서 감사합니다. 나는 내가 어떻게 보지 못했는지 모른다. 그것은 아주 잘 작동합니다. – fthopkins

+1

@ user3583237 http://developer.android.com/reference/android/os/AsyncTask.html#get(). 나는이 실수를하게 한 asynctask의 사용을 오해했다고 생각합니다. – Raghunandan

+0

예, AsynTask 프로세스를 오해했는데, 이제는 이해했습니다. 다시 감사합니다. – fthopkins

1

AsyncTask을 사용할 때는 항상 get()으로 전화하지 않아야합니다. 대신 모든 후 처리 작업을 수행하십시오. onPostExecute

@Override 
protected void onPostExecute(String result2) { 
    super.onPostExecute(result2); 
    try { 
     JSONArray jsonResponse2 = new JSONArray(result2); 
     asd2 = new String[3][jsonResponse2.length()]; 
     if (asd2[0][0]!=null && asd2[1][0]!=null) { 
      if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi)) { 
       // Codes 
      } 
     } 
    } 

    dialog.dismiss(); 
} 
+1

네가 완전히 옳다. 나는 AsyncTask를 기다리려고했는데, 아마도 그게 효과가있을 것이라고 생각했지만, didnt. 감사합니다. 아주 좋은 지적입니다. 'get()'을 사용할 필요가 없습니다. – fthopkins

관련 문제