完成前一个后执行一个Asyntask

时间:2015-06-29 07:44:43

标签: android facebook

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

但是第二次asyntask没有被解雇。但是,如果我从其他地方手动调用它,则会触发,但由于它在第一次执行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时在logcat中打印的内容

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()方法永远不会被触发,所以其余的代码永远不会被执行。但我无法理解为什么它不会被解雇!!!

这是第二个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");
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

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

您可以连接多个来电,进行不同的回调(取决于它是否成功,失败或总是必须运行),并且可以更好地控制您的来电。