2012-11-14 3 views
0

J'ai une méthode run() et dans ce que j'exécute une asynctask qui fait HTTPGET. Comment puis-je faire run() attendre l'asynctask pour obtenir des résultats et continuer?Méthode de suspension qui exécute asynctask HTTPGET

+0

Vous devez fournir plus d'informations dans votre question. Est-ce que la méthode 'run()' s'exécute dans un thread séparé? –

Répondre

0
//like this 
    asyncRequest(URL, parameters, "POST",new RequestListener() { 
       @Override 
       public void onComplete(String response) { 
        /*@todo get results and then continue */ 
       } 

       @Override 
       public void onIOException(IOException ex) { 
       } 

       @Override 
       public void onFileNotFoundException(FileNotFoundException ex) { 
       } 

       @Override 
       public void onMalformedURLException(MalformedURLException ex) { 
       } 
      }); 

public static void asyncRequest(final String requestUrl,final Bundle parameters,final String httpMethod,final RequestListener requestListener) { 
     new Thread() { 
      public void run() { 
       try { 
        String resp = MyUtil.openUrl(requestUrl, httpMethod, parameters); 
        requestListener.onComplete(resp); 
       } catch (FileNotFoundException ex) { 
        requestListener.onFileNotFoundException(ex); 
          } catch (MalformedURLException ex) { 
           requestListener.onMalformedURLException(ex); 
          } catch (IOException ex) { 
           requestListener.onIOException(ex); 
          } 
      } 
     }.start(); 
} 

public static interface RequestListener { 
    public void onComplete(String response); 

    public void onIOException(IOException ex); 

    public void onFileNotFoundException(FileNotFoundException ex); 

    public void onMalformedURLException(MalformedURLException ex); 
} 
-1

exécuter la fonction d'exécution dans la méthode onPostExecute du AsyncTask

private class LongOperation extends AsyncTask<String, Integer, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      //do long asynctask activity here 
     }  

     @Override 
     protected void onPostExecute(String result) { 
      run(); //executes the run method you want here after asysnctask is completed 
     } 

     @Override 
     protected void onPreExecute() { 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
     } 
} 
+0

c'est ce que je pensais, mais la méthode d'exécution est en cours d'exécution à partir d'un broadcastReceiver qui passe Context, et les chaînes – synjunked

Questions connexes