title: How do I return value from AsyncTask class to another class?
body: <p>I have an option menu item that allows a user to see their current location (based on Zip Code) on <strong>Google Maps</strong> using an <strong>intent</strong>. Because Google Maps only accepts Lat/Lng, I am using the <strong>Geocoding API</strong> to return Lat/Lng in <strong>JSON</strong> format. Here is the code that executes once the user selects the menu item:</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>@Override public boolean onOptionsItemSelected(MenuItem item) {
  
  int id = item.getItemId();
  
  if (id == R.id.action_settings) {
    Intent in = new Intent(this, SettingsActivity.class);
    startActivity(in);
    return true;
  }
  if (id == R.id.action_map) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String location = prefs.getString(getString(R.string.pref_location_key),
        getString(R.string.pref_location_default));
    FetchZipTask fzt = new FetchZipTask();
    fzt.execute(location);
    loc = fzt.locale;
    Uri geoLocation = Uri.parse("geo:"+ loc);
    Log.d("Debug", geoLocation.toString());
    Intent in = new Intent(Intent.ACTION_VIEW);
    in.setData(geoLocation);
    if (in.resolveActivity(getPackageManager()) != null) {
      startActivity(in);
    }
  }
  
  return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>I am currently trying to use a public String field in the <strong>AsyncTask</strong> class that is updated when the <code>onPostExecute()</code> method parses the <strong>JSON</strong> and formats the retrieved Lat/Lng string. I then access this public field from the <strong>MainActivity</strong> class whenever the user selects the menu item, but the field is always null. What am I doing wrong, and is it the most effective way to leverage <strong>AsyncTask</strong>? I'm thinking there must be a better way to return the value. </p>
  
  <p><strong>FetchZipTask.java</strong></p>
  
  <pre><code>public class FetchZipTask extends AsyncTask&lt;String, Void, String&gt; {
  public String locale = null;
  
  @Override protected void onPostExecute(String result) {
  locale = result;
  }
  
  @Override protected String doInBackground(String... params) {
  
  if (params.length == 0) {
    return null;
  }
  
  HttpURLConnection urlConnection = null;
  BufferedReader reader = null;
  
  //raw JSON response as a string
  String locationJsonStr = null;
  
  try {
    final String BASE_LOCATION_URL =       
  
  
  "https://maps.googleapis.com/maps/api/geocode/json?";
    final String ADDRESS_PARAM = "address";
    final String APPID_PARAM = "key";
  
    // URI.path vs URI.parse vs. URI Scheme
    Uri builtUri = Uri.parse(BASE_LOCATION_URL)
        .buildUpon()
        .appendQueryParameter(ADDRESS_PARAM, params[0])
        .appendQueryParameter(APPID_PARAM, BuildConfig.GOOGLE_GEOCODE_API_KEY)
        .build();
    //Log.d("Debug", builtUri.toString());
  
    URL url = new URL(builtUri.toString());
  
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.connect();
  
    // Read the input stream into a String
    InputStream inputStream = urlConnection.getInputStream();
    StringBuffer buffer = new StringBuffer();
    if (inputStream == null) {
      // Nothing to do.
      return null;
    }
    reader = new BufferedReader(new InputStreamReader(inputStream));
  
    String line;
    while ((line = reader.readLine()) != null) {
  
      // buffer for debugging.
      line.concat(" Hello ");
      line.concat("\n");
      buffer.append(line);
    }
  
    if (buffer.length() == 0) {
      // Stream was empty.  No point in parsing.
      return null;
    }
    locationJsonStr = buffer.toString();
  
    Log.v("debug", "Location string: " + locationJsonStr);
   } catch (IOException e) {
  
    return null;
   } finally {
    if (urlConnection != null) {
      urlConnection.disconnect();
    }
    if (reader != null) {
      try {
        reader.close();
      } catch (final IOException e) {
        Log.e("ForecastFragment", "Error closing stream", e);
      }
    }
  }
  try {
    return getLocationDataFromJson(locationJsonStr);
  } catch (JSONException e) {
    e.printStackTrace();
  }
  return null;
  }
  
  private String getLocationDataFromJson(String forecastJsonStr) throws          
  JSONException {
  // These are the names of the JSON objects that need to be extracted.
  final String GEO_LIST = "results";
  final String GEO_OBJ = "geometry";
  final String GEO_LOC = "location";
  final String GEO_LAT = "lat";
  final String GEO_LNG = "lng";
  
  JSONObject forecastJson = new JSONObject(forecastJsonStr);
  JSONArray resultsArray = forecastJson.getJSONArray(GEO_LIST);
  JSONObject resultsObj = resultsArray.getJSONObject(0);
  JSONObject geoObj = resultsObj.getJSONObject(GEO_OBJ);
  JSONObject latLng = geoObj.getJSONObject(GEO_LOC);
  String lat = latLng.getString(GEO_LAT);
  String lng = latLng.getString(GEO_LNG);
  
  Log.d("location", "Lat:" + lat + "\n Lng:" + lng);
  
  return lat + "," + lng;
  }
  }
  </code></pre>