1

I have used AsyncTask with Activity, And it gave me desired result without any failure. Now I need to use AsyncTask with Fragments. In Fragments AsyncTask is not updating UI. I got an answer here. I tried that logic in my code but still I'm not able to update UI after getting response from servlet. Your help will be very appreciated.

Code what I have tried:

public class FragmentMyProfile extends Fragment
{
    TextView txtViewUserFullName;

    SharedPreferences shrdPref;

    String currentUserFirstName = "", currentUserEmail = "";
    String URL = "http://10.0.2.2:8080/iGnite_Survey/GetUserDetailsServlet";

    String jsonObjectReceivedFromServer = "";


    public FragmentMyProfile() 
    {
        // empty constructor required for fragment subclass
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View rootView = inflater.inflate(R.layout.fragment_my_profile, container, false);


        shrdPref = rootView.getContext().getSharedPreferences("shrdPref", Context.MODE_PRIVATE);

        txtViewUserFullName = (TextView) rootView.findViewById(R.id.textViewUserFullName);



        //get current user first name, default value is ""
        currentUserFirstName = shrdPref.getString(String.valueOf(R.string.curentLoggedInUserFirstName), "");
        currentUserEmail = shrdPref.getString(String.valueOf(R.string.curentLoggedInUserEmail), "");


        //display current user first name
        if(!currentUserFirstName.equals(""))
        {
            txtViewDisplayUserFirstName.setText("Welcome "+currentUserFirstName);
        }
        else
        {
            txtViewDisplayUserFirstName.setText(String.valueOf(R.string.welcomeUser));
        }


        //get all user details from server
        GetUserDetailsAsyncTask getUserDetailsAsyncTask = new GetUserDetailsAsyncTask (new FragmentCallback() 
        {
            @Override
            public void onTaskDone(String output) 
            {
                //txtViewUserFullName.setText(output);
            }
        });
        getUserDetailsAsyncTask.execute(new String[] { URL });



        return rootView;
    }





    public interface FragmentCallback
    {
        public void onTaskDone(String output);
    }



    //------------------------------------------------------------------------------
    public class GetUserDetailsAsyncTask extends AsyncTask<String, Void, String> 
    {
        private FragmentCallback mFragmentCallback;

        public GetUserDetailsAsyncTask (FragmentCallback fragmentCallback)
        {
            mFragmentCallback = fragmentCallback;
        }


        @Override
        protected String doInBackground(String... urls) 
        {
            String output = null;
            for (String url : urls) 
            {
                output = sendDataToServer(url);
            }
            return output;
        }


        @Override
        protected void onPostExecute (String output)
        {
            super.onPostExecute(output);
            mFragmentCallback.onTaskDone();
            txtViewUserFullName.setText("output");
        }


        private String sendDataToServer(String url)
        {
            String output = null;

            try 
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

                nameValuePairs.add(new BasicNameValuePair("user_email", currentUserEmail));

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity httpEntity = response.getEntity();
                output = EntityUtils.toString(httpEntity);
            } 
            catch (ClientProtocolException e)
            {
                    e.printStackTrace();
            } 
            catch (IOException e) 
            {
                    e.printStackTrace();
            }
            return output;
        }
    }
}
Community
  • 1
  • 1
Sanjay Joshi
  • 2,036
  • 6
  • 33
  • 48

1 Answers1

0

You have to use the inter-fragment communication concept to update the UI Thread from the fragment asynctask i.e INTERFACE

Please refer this tutorial with source code to understand it better : Handle Android AsyncTask Configuration Change Using Fragment

prkmathur
  • 11
  • 3