Question

I've been studying a lot about Android development recently and to really get the feeling of it, I want to start building a simple application that would get me going. Unfortunately, I think the more I read, the more confused I get.

I want to build an app that scans and reads a bar code, queries a RESTful web service with the EAN read from the bar code and outputs the name of the product.

Now, the scanning part was easy, as I used the zxing library as an intent service. Then, I played a lot with a framework called Volley which is supposed to handle the network communication with the web service and parsing the JSON result. Unfortunately, I wasn't able to integrate Volley with my app, but maybe this handy tool is more than I actually need.

What would be the right approach to achieve the above goal? Do I need a content provider? Do I need a service? How would these relate to each other?

Was it helpful?

Solution

I haven't worked with Volley myself, so I can't give you an answer if it's too advanced for what you want to achieve. In general when it comes to HTTP communication with a server I prefer to use AndroidAsyncHttpClient:

"An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing."

Example relevant to what you want to do:

public class YourActivity extends Activity {

    private void handleScannedBardcode(String barcode) {
        // you need to make the request match the REST API you are using
        RequestParams params = new RequestParams();
        params.put("A_KEY_TO_IDENTIFY_THE_PARAMETER", barcode);

        AsyncHttpClient client = new AsyncHttpClient();
        client.post("http://www.yourserver.com", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                // you need to add parsing of JSON data to match the response
                JSONObject jo = new JSONObject(response);
                String productName = jo.getString("productname");
                updateProductView(productName);
            }
        }); 
    }

    private void updateProductView(String productName) {
        // you need to use a view id that corresponds to a textview in your layout xml 
        TextView tv = (TextView)findViewById(R.id.productName);
        tv.setText(productName);
    }
}

Depending on how complex the JSON response is you can either opt for GSON or Jackson for parsing of large amounts of JSON or plain JSONObject

OTHER TIPS

I use this library to handle the communication with my API: https://github.com/koush/ion

It's easy to use, has samples and code examples.

Go with simple solution first:

  • use HttpURLConnection to connect with web service,
  • then use either InputStreamReader or BufferedInputStream to read the input stream from the connection.
  • Create a JSON model class similar to the one you get from your web service.
  • Use Google GSON library to parse the response into a JSONResponse and then use the data.

I'm sure you'll be able to find enough help on SO regarding these individual points to get you started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top