Android Open Source - androidui Place Downloader Task






From Project

Back to project page androidui.

License

The source code is released under:

MIT License

If you think the Android project androidui listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package course.labs.placebadges_prel;
/*w w  w  .j a v a  2s.c  o  m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.util.Log;

public class PlaceDownloaderTask extends AsyncTask<Location, Void, PlaceRecord> {

  // Change to false if you don't have network access
  private static final boolean HAS_NETWORK = true;

  // TODO - put your www.geonames.org account name here.
  private static String USERNAME = "aporter";

  private HttpURLConnection mHttpUrl;
  private WeakReference<PlaceViewActivity> mParent;
  private static Bitmap mStubBitmap;
  private static Location mockLoc1 = new Location(
      LocationManager.NETWORK_PROVIDER);
  private static Location mockLoc2 = new Location(
      LocationManager.NETWORK_PROVIDER);

  public PlaceDownloaderTask(PlaceViewActivity parent) {
    super();
    mParent = new WeakReference<PlaceViewActivity>(parent);

    if (!HAS_NETWORK) {
      mStubBitmap = BitmapFactory.decodeResource(parent.getResources(),
          R.drawable.stub);
      mockLoc1.setLatitude(37.422);
      mockLoc1.setLongitude(-122.084);
      mockLoc2.setLatitude(38.996667);
      mockLoc2.setLongitude(-76.9275);
    }
  }

  @Override
  protected PlaceRecord doInBackground(Location... location) {

    PlaceRecord place = null;

    if (HAS_NETWORK) {

      place = getPlaceFromURL(generateURL(USERNAME, location[0]));

      if ("" != place.getCountryName()) {
        place.setLocation(location[0]);
        place.setFlagBitmap(getFlagFromURL(place.getFlagUrl()));
      } else {
        place = null;
      }

    } else {
      place = new PlaceRecord(location[0]);
      if (location[0].distanceTo(mockLoc1) < 100) {
        place.setCountryName("United States");
        place.setPlace("The Greenhouse");
        place.setFlagBitmap(mStubBitmap);
      } else {
        place.setCountryName("United States");
        place.setPlace("Berwyn");
        place.setFlagBitmap(mStubBitmap);
      }
    }

    return place;

  }

  @Override
  protected void onPostExecute(PlaceRecord result) {

    if (null != result && null != mParent.get()) {
      mParent.get().addNewPlace(result);
    }
  }

  private PlaceRecord getPlaceFromURL(String... params) {
    String result = null;
    BufferedReader in = null;

    try {

      URL url = new URL(params[0]);
      mHttpUrl = (HttpURLConnection) url.openConnection();
      in = new BufferedReader(new InputStreamReader(
          mHttpUrl.getInputStream()));

      StringBuffer sb = new StringBuffer("");
      String line = "";
      while ((line = in.readLine()) != null) {
        sb.append(line + "\n");
      }
      result = sb.toString();

    } catch (MalformedURLException e) {

    } catch (IOException e) {

    } finally {
      try {
        if (null != in) {
          in.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      mHttpUrl.disconnect();
    }

    return placeDataFromXml(result);
  }

  private Bitmap getFlagFromURL(String flagUrl) {

    InputStream in = null;

    Log.i("temp", flagUrl);

    try {
      URL url = new URL(flagUrl);
      mHttpUrl = (HttpURLConnection) url.openConnection();
      in = mHttpUrl.getInputStream();

      return BitmapFactory.decodeStream(in);

    } catch (MalformedURLException e) {
      Log.e("DEBUG", e.toString());
    } catch (IOException e) {
      Log.e("DEBUG", e.toString());
    } finally {
      try {
        if (null != in) {
          in.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
      mHttpUrl.disconnect();
    }

    return BitmapFactory.decodeResource(mParent.get().getResources(),
        R.drawable.stub);
  }

  private static PlaceRecord placeDataFromXml(String xmlString) {
    DocumentBuilder builder;
    String countryName = "";
    String countryCode = "";
    String placeName = "";

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
      builder = factory.newDocumentBuilder();
      Document document = builder.parse(new InputSource(new StringReader(
          xmlString)));
      NodeList list = document.getDocumentElement().getChildNodes();
      for (int i = 0; i < list.getLength(); i++) {
        Node curr = list.item(i);

        NodeList list2 = curr.getChildNodes();

        for (int j = 0; j < list2.getLength(); j++) {

          Node curr2 = list2.item(j);
          if (curr2.getNodeName() != null) {

            if (curr2.getNodeName().equals("countryName")) {
              countryName = curr2.getTextContent();
            } else if (curr2.getNodeName().equals("countryCode")) {
              countryCode = curr2.getTextContent();
            } else if (curr2.getNodeName().equals("name")) {
              placeName = curr2.getTextContent();
            }
          }
        }
      }
    } catch (DOMException e) {
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return new PlaceRecord(generateFlagURL(countryCode.toLowerCase()),
        countryName, placeName);
  }

  private static String generateURL(String username, Location location) {

    return "http://www.geonames.org/findNearbyPlaceName?username="
        + username + "&style=full&lat=" + location.getLatitude()
        + "&lng=" + location.getLongitude();
  }

  private static String generateFlagURL(String countryCode) {
    return "http://www.geonames.org/flags/x/" + countryCode + ".gif";
  }

}




Java Source Code List

com.example.aporter.helloandroidwithimageview.HelloAndroidWithImageViewActivity.java
course.examples.Fragments.DynamicLayout.QuoteViewerActivity.java
course.examples.Fragments.DynamicLayout.QuotesFragment.java
course.examples.Fragments.DynamicLayout.TitlesFragment.java
course.examples.Notification.StatusBarWithCustomView.NotificationSpecialActivity.java
course.examples.Notification.StatusBarWithCustomView.NotificationStatusBarWithExpandedViewActivity.java
course.examples.Notification.Toast.NotificationToastActivity.java
course.examples.Notification.ToastWithCustomView.NotificationToastActivity.java
course.examples.UI.AlertDialog.AlertDialogActivity.java
course.examples.UI.AutoComplete.AutoCompleteActivity.java
course.examples.UI.Button.ButtonActivity.java
course.examples.UI.CheckBox.CheckBoxActivity.java
course.examples.UI.GridView.GridLayoutActivity.java
course.examples.UI.GridView.ImageAdapter.java
course.examples.UI.GridView.ImageViewActivity.java
course.examples.UI.LinearLayout.LinearLayoutActivity.java
course.examples.UI.ListLayout.ListViewActivity.java
course.examples.UI.ListLayout.ListViewAdapter.java
course.examples.UI.MapView.GoogleMapActivity.java
course.examples.UI.RadioGroup.RadioGroupActivity.java
course.examples.UI.RatingsBar.RatingsBarActivity.java
course.examples.UI.RecyclerView.MyRecyclerViewAdapter.java
course.examples.UI.RecyclerView.RecyclerViewActivity.java
course.examples.UI.RelativeLayout.RelativeLayoutActivity.java
course.examples.UI.Spinner.SpinnerActivity.java
course.examples.UI.TabLayout.GridFragment.java
course.examples.UI.TabLayout.ImageAdapter.java
course.examples.UI.TabLayout.ImageViewActivity.java
course.examples.UI.TabLayout.TabLayoutActivity.java
course.examples.UI.TableLayout.TableLayoutActivity.java
course.examples.UI.ViewPager.GalleryWithViewPagerActivity.java
course.examples.UI.ViewPager.ImageAdapter.java
course.examples.UI.ViewPager.ImageHolderFragment.java
course.examples.UI.WebView.WebViewActivity.java
course.examples.UI.datepicker.DatePickerFragmentActivity.java
course.examples.UI.timepicker.TimePickerFragmentActivity.java
course.examples.UI.togglebutton.ToggleButtonActivity.java
course.examples.colorpalettewithnavdrawer.ApplicationTest.java
course.examples.colorpalettewithnavdrawer.DisplayColorActivity.java
course.examples.colorpalettewithnavdrawer.DisplaySingleColorActivity.java
course.examples.colorpalettewithnavdrawer.PaletteNameAdapter.java
course.examples.colorpalettewithswipe.ApplicationTest.java
course.examples.colorpalettewithswipe.DisplayColorPaletteActivity.java
course.examples.colorpalettewithswipe.DisplaySingleColorActivity.java
course.examples.colorpalettewithswipe.PaletteAdapter.java
course.examples.fragments.StaticLayout.QuoteViewerActivity.java
course.examples.fragments.StaticLayout.QuotesFragment.java
course.examples.fragments.StaticLayout.TitlesFragment.java
course.examples.fragments.staticconfiglayout.QuoteViewerActivity.java
course.examples.fragments.staticconfiglayout.QuotesFragment.java
course.examples.fragments.staticconfiglayout.TitlesFragment.java
course.examples.helloandroidwithlogin.ApplicationTest.java
course.examples.helloandroidwithlogin.HelloAndroidWithImageViewActivity.java
course.examples.helloandroidwithlogin.LoginActivity.java
course.examples.modernartpiano.MainActivity.java
course.examples.modernartui.MainActivity.java
course.examples.notification.StatusBar.NotificationStatusBarActivity.java
course.examples.notification.StatusBar.NotificationSubActivity.java
course.examples.quoteviewer.QuoteListActivity.java
course.examples.quoteviewer.TitlesListActivity.java
course.examples.ui.fragmentactionbar.QuoteFragment.java
course.examples.ui.fragmentactionbar.QuoteViewerActivity.java
course.examples.ui.fragmentactionbar.TitlesFragment.java
course.examples.ui.helloworldwithmenus.HelloAndroidWithMenuActivity.java
course.labs.multipane.MainActivity.java
course.labs.multipane.QuoteFragment.java
course.labs.multipane.TitlesFragment.java
course.labs.placebadges.MockLocationProvider.java
course.labs.placebadges.PlaceDownloaderTask.java
course.labs.placebadges.PlaceRecord.java
course.labs.placebadges.PlaceViewActivity.java
course.labs.placebadges.PlaceViewAdapter.java
course.labs.placebadges.PlaceViewDetailActivity.java
course.labs.placebadges_prel.MockLocationProvider.java
course.labs.placebadges_prel.PlaceDownloaderTask.java
course.labs.placebadges_prel.PlaceRecord.java
course.labs.placebadges_prel.PlaceViewActivity.java
course.labs.placebadges_prel.PlaceViewAdapter.java
course.labs.placebadges_prel.PlaceViewDetailActivity.java
examples.course.basiccolorpalette.ApplicationTest.java
examples.course.basiccolorpalette.DisplayColorActivity.java
examples.course.basiccolorpalette.DisplayColorNames.java
examples.course.basiccolorpalette.DisplaySingleColorActivity.java
examples.course.basiccolorpalette.PaletteAdapter.java
examples.course.basiccolorpaletteupnav.ApplicationTest.java
examples.course.basiccolorpaletteupnav.DisplayColorActivity.java
examples.course.basiccolorpaletteupnav.DisplayColorNamesActivity.java
examples.course.basiccolorpaletteupnav.DisplaySingleColorActivity.java
examples.course.basiccolorpaletteupnav.PaletteAdapter.java
examples.course.ticker.TickerDisplayActivity.java
examples.course.uicardview.ApplicationTest.java
examples.course.uicardview.CardViewActivity.java