简体   繁体   中英

Which is the correct way to call async task in onCompleted method in facebook 3.0 Request.newMeRequest method

I tried this, but I'm getting WindowLeaked error message after postexecute method is called.

here Is my code:

    Request meRequest=Request.newMeRequest(session, new GraphUserCallback()
                {

                        @Override
                        public void onCompleted(GraphUser user, Response response)
                        {
                            if(response.getError()==null)
                            {

                                try 
                                {


                                    MyAsyncTask async = new MyAsyncTask ();
                                    async.execute("fbsignup");

                                } catch (Exception e) 
                                {
                                   Log.v("FB  error:::::::::", e.getMessage());
                                }

                            }
                        }

                });
     meRequest.executeAsync();

This is MyAsyncTask class, When I tried to call Intent without using asynctask it is working fine, so i guess my error is in asynctask only.

private class MyAsyncTask extends AsyncTask<String, Void, Void> {

    private ProgressDialog pDialog = null;
    private String responseFromServer = null;
    private boolean hasExceptionOccured = false;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Please wait..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

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

        try 
        {

            // Parse graph user data and check whether user has registered or not.
            // If user is not registered mandatory password popup. 

            ServiceHandler handler = new ServiceHandler();
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();

            if(params[0].equalsIgnoreCase("fbsignup"))
            {

                parameters.add(new BasicNameValuePair("email", UserEmailID));   

                responseFromServer = handler.makeServiceCall(URL, 2, parameters);

            }

        } catch (Exception e) 
        {
             Log.v("CLassName::::::SignupwithEmail:::::::AsyncTask", e.getMessage());
             hasExceptionOccured = true; 
        }

        return null;
    }


     @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (pDialog!=null)
            { 
                pDialog.dismiss();
            }

            try 
            {   

                    if(responseFromServer.contains("success"))
                    {
                    Intent i = new Intent(getActivity(), HomePage.class);
                    getActivity().startActivity(i);
                    getActivity().finish(); 
                    }

            } catch (Exception e) 
            {
                Log.v("Main FRagment FB async::::::", e.getMessage());
            }   

        }    

}

hey you can use updated facebook v3.6 sdk. Please download and get some sample app also available inside. Facebook v3.6

user after login session

Session session = Session.getActiveSession();

requestMyAppFacebookFriends(session);


 private List<GraphUser> getResults(Response response) {
            GraphMultiResult multiResult = response
                    .getGraphObjectAs(GraphMultiResult.class);
            GraphObjectList<GraphObject> data = multiResult.getData();
            return data.castToListOf(GraphUser.class);
    }
     private void requestMyAppFacebookFriends(Session session) {
            Request friendsRequest = createRequest(session);
            friendsRequest.setCallback(new Request.Callback() {

                @Override
                public void onCompleted(Response response) {

                  //List<GraphUser> friends = getResults(response);

                  //Log.e("RESULT : ", "@"+friends.size());

                // TODO: your code here

                   }
            });
            friendsRequest.executeAsync();
        }
     private Request createRequest(Session session) {
            Request request = Request.newGraphPathRequest(session, "me/friends", null);

            Set<String> fields = new HashSet<String>();
            String[] requiredFields = new String[] { "id", "name", "picture","installed" };
            fields.addAll(Arrays.asList(requiredFields));

            Bundle parameters = request.getParameters();
            parameters.putString("fields", TextUtils.join(",", fields));
            request.setParameters(parameters);

            return request;
        }

I added

   Looper.loop(); 

after

  async.execute("fbsignup");

This solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM