0

I'm still learning how to extract data from a website and I really hope I'll get some nice answers adequate for a starter. Anyways, my goal here is to extract the data in the background of my app(without openning and showing it in my app). The idea is that data then would be stored for later use. The API I'm using has 2 GetMethods: GetProductJSON(which has JSON Response) and GetProduct(with a Comma Seperated Values(CSV) Response) Here is an example of the JSON Response website:

     {"0":{"productname":"Neutrogena Lips Stick 4.8g","imageurl":"http://ecx.images-  amazon.com/images/I/31E1ct854gL._SL160_.jpg","producturl":"","price":"5.65","currency":"USD","saleprice":"","storename":"N/A"}}

The Comma Seperated Values Response looks like this:

     "productname","imageurl","producturl","price","currency","saleprice","storename"
     "Neutrogena Lips Stick 4.8g","http://ecx.images-amazon.com/images/I/31E1ct854gL._SL160_.jpg","","5.65","USD","","N/A"

Here is how I call the website:

    url = url.replace("{CODE}", codeValue);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    cardPresenter.setPendingIntent(createPendingIntent(getContext(), intent));

Any suggestions on how to make this a background task and how to actually get the data in java so that I can use them on a Livecard.

Emir
  • 13
  • 8

4 Answers4

2

First, you need to have access to the Internet. Include the following permission into your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

Using GDK and AsyncTask:

import android.os.AsyncTask;

public class RetrieveData extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... resource) {
        String data;
        try {
            URL url = new URL(resource[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(connection.getInputStream());
            data = convertStreamToString(in);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    private String convertStreamToString(InputStream in) {
        Scanner s = new Scanner(in);
        return s.useDelimiter("\\A").hasNext() ? s.next() : "";
    }
}

The method convertStreamToString() is described there.

In your Service or Activity:

String retrievedData;
try {
    retrievedData = new RetrieveData().execute("http://www.example.com/GetProductJSON").get();
} catch (Exception e) {
    e.printStackTrace();
}
// Process data

Hope that helps.

Community
  • 1
  • 1
patapizza
  • 2,366
  • 13
  • 14
0

You have at least these choices using GDK: 1) create an asynch task; or 2) create a private service that you assign tasks to periodically.

ErstwhileIII
  • 4,691
  • 2
  • 21
  • 36
0

The first thing you will need to do in your Android Application is to add permission to access the Internet in your AndroidManifest.xml file. Add this tag as a sibling of <application>.

<uses-permission android:name="android.permission.INTERNET"/>

You will then need to consider in what manner you will make the HTTP requests. In an ideal application, you need to use the AsyncTask class to make these requests to avoid blocking the UI thread.

If you are just looking for a quick proof of concept, you can permit these requests on the UI thread by modifying your policy. Add this code to your onCreate() method in the MainActivity

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

This is not an advisable long term solution since it blocks the UI thread, but there are StackOverflow topics on it. How to fix android.os.NetworkOnMainThreadException?.

Here is another article on the concept of an AsyncTask http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html.

Community
  • 1
  • 1
asporter
  • 71
  • 7
0

Although you seem to be indicating you're using the GDK, you may want to consider a server-push via the Mirror API instead. Since you need to fetch the information via the network anyway, you're losing out on many of the advantages the GDK offers.

With the Mirror API, you would create a new timeline item with timeline.insert and save the id of the card that was created. You would probably want to give your user the option to pin the card so it is placed in the "now" area of the timeline.

When updating, you can call timeline.update with the new information.

Keep in mind that you do need to update the card periodically or it may fall off the timeline or out of the pinned area after seven days of inactivity.

Prisoner
  • 48,391
  • 6
  • 48
  • 97