2015-06-29 116 views
-1

我需要能够在另一个完成之后执行一个asyntask。我试图通过在第一个方法的onPostExecute()方法中发射第二个asyntask来做到这一点。在完成之前执行一个Asyntask

但第二ascombask没有被解雇。但是,如果我从别处手动调用它,它会触发它,但是因为它试图在第一个asyntask执行后发布值,所以它会尝试发送空值。

这里是我的代码的相关部分

//Asynctask to get Getting fb profile details 
     private class FetchOperation extends AsyncTask<Void, Void, String> { 
      String fb_token; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       // Get user defined values 
       fb_token = token; 


      } 

      @Override 
      protected String doInBackground(Void... params) { 
       String response = ""; 
       String Urls = "https://graph.facebook.com/me?access_token="; 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpGet httpget = new HttpGet(Urls +token); 
       HttpEntity httpEntity = null; 
       HttpResponse httpResponse = null; 
       try { 
        httpResponse = httpclient.execute(httpget); 

       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
        Log.v("Response", "Hi From e1 : " + e.toString()); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       try { 
        httpEntity = httpResponse.getEntity(); 
        response = EntityUtils.toString(httpEntity); 
        Log.v("Response", "Hi From 2 : "+response.toString()); 
        return response; 
       } catch (IOException e) { 
        e.printStackTrace(); 
        Log.v("Response", "Hi From e2 : " + e.toString()); 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(String jsonStr) { 
       super.onPostExecute(jsonStr); 
       Log.v("tag", "Result:\n" + jsonStr); 
       if (jsonStr != null) { 
        try{ 
         JSONObject jsonObj = new JSONObject(jsonStr); 
         String email = jsonObj.getString("email"); 
         String firstName = jsonObj.getString("first_name"); 
         String lastName = jsonObj.getString("last_name"); 
         String gender = jsonObj.getString("gender"); 
         String country = jsonObj.getString("locale"); 
         id = jsonObj.getString("id"); 
         user = firstName.concat(" "); 
         user = user.concat(lastName); 
         image = "http://graph.facebook.com/" + id + "/picture?type=large"; 
Log.v("Fb name", "Testing Name : " + user); 
         new UploadOperation().execute(); 

        } 
        catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
       else { 
        Log.e("ServiceHandler", "Couldn't get any data from the url"); 
       } 
      } 
     } 

这是当这个Asyntask被执行

06-29 13:06:14.707 6396-6396/com.example.kmi_dev.fbloginsample V/tag﹕ Token: 
    CA******************************************************************************************************G 
06-29 13:06:15.427 6396-6438/com.example.kmi_dev.fbloginsample V/Response﹕ Hi From 2 : {"id":"910************","first_name":"S********","gender":"male","last_name":"Verma","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/910*********\/","locale":"en_GB","name":"S******* Verma","timezone":5.5,"updated_time":"2015-06-22T04:17:39+0000","verified":true} 

我做了一些周围挖掘,并发现该onPostExecute()在logcat中得到什么印刷方法永远不会被触发,所以剩下的代码永远不会被执行。但我不明白为什么它不会被解雇!

这是第二Asyntask的代码,这一个试图火

//Asynctask to save fb details 
    private class UploadOperation extends AsyncTask<Void, Void, String> { 
     String api_key, user_name, img_path; 
     ProgressDialog dialog; 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Get user defined values 
       api_key = id; 
       user_name = user; 
       img_path = image; 

     //Initiate ProgressBar 
      dialog = ProgressDialog.show(MainActivity.this, "Please Wait", "Logging you in ..."); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      String response = ""; 
      String Urls = "http://192.168.100.51/task_manager/v1/"; 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(Urls +"facebookid"); 
      HttpEntity httpEntity = null; 
      HttpResponse httpResponse = null; 
      try { 
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 

       nameValuePairs.add(new BasicNameValuePair("fb_name", user_name)); 
       nameValuePairs.add(new BasicNameValuePair("fb_id", api_key)); 
       nameValuePairs.add(new BasicNameValuePair("fb_img_path", image)); 

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       httpResponse = httpclient.execute(httppost); 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
       Log.v("Response", "Hi From e1 : " + e.toString()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       httpEntity = httpResponse.getEntity(); 
       response = EntityUtils.toString(httpEntity); 
       Log.v("Response", "Hi From 2 : "+response.toString()); 
       return response; 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Log.v("Response", "Hi From e2 : " + e.toString()); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String jsonStr) { 
      super.onPostExecute(jsonStr); 
      dialog.dismiss(); 
      Log.v("tag", "Result:\n" + jsonStr); 
      if (jsonStr != null) { 
       try{ 
        JSONObject jsonObj = new JSONObject(jsonStr); 
        String message = jsonObj.getString("message"); 
        boolean error = jsonObj.getBoolean("error"); 
        error(error, message, api_key); 
       } 
       catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 
     } 
    } 
+0

我想你没有收到来自Url的回应,或者网络中可能有错误,你是否检查过你是否得到回应? – Kartheek

+0

@Kartheek我收到URL的回应等待让我更新问题 –

+0

@hoomi Asyntask你在说什么? 'FetchOperation'一个?我没有任何异常,我的logcat也不明白为什么我需要在第一个“错误”和“消息”值。能否请你用代码 –

回答

1

为您提供完美的解决方案是使用http://jdeferred.org/

你可以将多个电话,有不同的回调(取决于它是否成功,失败或者总是需要运行的东西),并且可以更好地控制您的呼叫。

相关问题