2017-10-18 7 views
0

이미 토론이 있고 해결책을 찾기 위해 인터넷에서 많은 것을 찾았습니다. 다음에 get이 필요하기 때문에 하나씩 3 개를 호출합니다 (발리슛). 변수는 이전에 가져온 변수에 설정되었지만 여기서는 작동하지 않는 것 같습니다. 전체 프로세스를 디버깅 할 때 정상적으로 작동하지만 응용 프로그램을 정상적으로 실행하면 디버깅 할 때 얻을 수있는 데이터가 없습니다.비동기 작업을 하나씩 실행

가 지금은 ..

public class AsyncToken extends AsyncTask<String , Void, Void> { 

private PacketBuilder pb = new PacketBuilder(); 
private Context context; 
AsyncUser u = new AsyncUser(context); 

@Override 
protected Void doInBackground(String... params) { 

    while(task1finished == false) 
    { 
     try 
     { 
      Log.d("App", "Waiting for GetTask"); 
      Thread.sleep(1000); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    Log.d("App", "GetTask finished"); 
    // Do what ever you like 
    // ... 
    pb.Get("token", "", params[0], context); 

    return null; 
} 

protected void onPostExecute(String ... params) 
{ 
    task2finished = true; 

} 

public AsyncToken(Context context) 
{ 
    this.context = context; 
} 

} 

편집을이 모든 것을 작동하게하는 정적 부울 변수를 설정하려고했지만 아직까지 성공을 프로그래머없는 코드 : Code where i start the first asynctask, in the end i check if the object i should have gotten with get is there, if yes, i start another activity

first asynctask

last asynchtask

답변

2

비동기 작업 완료시 onPostExecute() 거기에서 계산 된 값을 가진 다른 비동기 작업을 시작하십시오. 전역 변수를 계산할 수 있거나 매개 변수를 통해 전달할 수 있습니다.

//start first async task 
new TaskOne().execute(); 


class TaskOne extends AsyncTask<Void,Void,String>{ 
    String tmp; 


    @Override 
    protected String doInBackground(Void... records) { 
     tmp = computeNeededValue(); 

     return tmp; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     new TaskTwo().execute(tmp); 
    } 
) 

class TaskTwo extends AsyncTask<String,Void,Void>{ 

    @Override 
    protected Void doInBackground(String... records) { 
     //use computed value from first task here 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     //start another async task 
    } 
) 
+0

나는이 솔루션을 시도하고이 변수가 내가 얻은 변수가 설정되었거나, 아무것도,이 오류를 얻지 못하는지 확인했다 : 가상 메소드 'java.lang.String org. json.JSONObject.getString (java.lang.String) '을 null 객체 참조에 추가합니다. 그리고 내가 잘못 디버깅 중이거나 onPostExecute에 뛰어 들지 않습니다. –

+0

@AlexandraK. 그 코드를 게시 할 수 있습니까? –

+0

편집, 첫 번째 작업은 첫 번째 작업의 실행입니다. 여기서도 모든 작업이 완료된 후 확인합니다. 예를 들어 새 작업을 시작하면 두 번째 그림은 마지막 작업이고 세 번째는 마지막 작업입니다. 태스크. –