0

I created an new AsyncTask and everything seems to be running fine. I just do not know how to retrieve the results and utilize them in the Fragment that is creating the AsyncTask.

How can I retrieve an ArrayList after the AsyncTask has finished running and use that ArrayList in the calling fragment? Is it within the OnPostExecute?

ASYNCTASK

    @Override
    protected ArrayList<String> doInBackground(String... params) {
        // TODO Auto-generated method stub
        mArray = new ArrayList<String>();
        StringBuilder sb = new StringBuilder(GEOCODING_API_BASE + OUT_JSON);
        try {
            sb.append("?address=")
                    .append(URLEncoder.encode(params[0], "UTF-8"));
            sb.append("&sensor=false");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String uri = sb.toString();

        HttpGet httpGet = new HttpGet(uri);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());

            double lng = ((JSONArray) jsonObject.get("results"))
                    .getJSONObject(0).getJSONObject("geometry")
                    .getJSONObject("location").getDouble("lng");

            double lat = ((JSONArray) jsonObject.get("results"))
                    .getJSONObject(0).getJSONObject("geometry")
                    .getJSONObject("location").getDouble("lat");

            Log.e("latitude", "" + lat);
            Log.e("longitude", "" + lng);
            String longitude = Double.toString(lng);
            String latitude = Double.toString(lat);
            mArray.add(longitude);
            mArray.add(latitude);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return mArray;
    }

    @Override
    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);

    }

}

CALLING FRAGMENT

case R.id.btnSubmit:
        String desc = etDesc.getText().toString().trim();
        String loc = etLocation.getText().toString().trim();
        String time = btnTime.getText().toString().trim();
        String date = btnDate.getText().toString().trim();
        String sport = tvSportName.getText().toString().trim();
        String title = etTitle.getText().toString().trim();
        ParseUser currentUser = ParseUser.getCurrentUser();

        // check to see if the values are empty

        SportEvent newSportEvent = new SportEvent();
        newSportEvent.setSport(sport);
        newSportEvent.setTime(title);
        newSportEvent.setDate(date);
        newSportEvent.setTitle(title);
        newSportEvent.setDesc(desc);
        newSportEvent.setUser(currentUser);

        //getLatLongFromAddress(loc);
        new LocationAsyncTask().execute(loc);

        // implement user signin first>>>

        // newSportEvent.setUser(ParseUser.getCurrentUser());
        ParseACL acl = new ParseACL();
        acl.setPublicReadAccess(true);
        acl.setPublicWriteAccess(true); // objects created are writable
        newSportEvent.setACL(acl);
Coova
  • 1,818
  • 5
  • 36
  • 63

1 Answers1

0

If your async task is a separate class, you could create an interface to send the results to the fragment. The async class would call an interface method and your fragment would implement the interface and override the interface method. See more HERE

Community
  • 1
  • 1
Shane
  • 972
  • 2
  • 12
  • 27