Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Product information like name, price etc using barcode number

I am developing an app which uses barcode to get the product information of items after scanning the barcode.

I don't want the user to install ZXing barcode app separately so I embedded the ZXing code into my project. So I was able to obtain the barcode ID number.

I want to get the product information like name, manufacturer, price etc using the bar code number using google search api for shopping.

Here is the code I have used

public class JSONExampleActivity extends Activity {

    TextView httpStuff; 
    DefaultHttpClient client; 
    JSONObject json;  

    final static String URL = "https://www.googleapis.com/shopping/search"; 
    String upc = "/v1/public/products?country=US&q=691464717759&restrictBy=gtin=691464717759";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);          

        httpStuff = (TextView) findViewById(R.id.tvHttp); 
        client = new DefaultHttpClient(); 
        new Read().execute("items");                
    }

    public JSONObject products(String upc)  throws ClientProtocolException, IOException, JSONException {     
        StringBuilder url = new StringBuilder(URL); 
        url.append(upc);
        HttpGet get = new HttpGet(url.toString());     
        HttpResponse r = client.execute(get);   
        int status = r.getStatusLine().getStatusCode(); 

        if (status == 200) {
            HttpEntity e = r.getEntity();         
            String data = EntityUtils.toString(e);         
            JSONObject timeline = new JSONObject(data); 

            return timeline;    
        } 
        else {         
            Toast.makeText(JSONExampleActivity.this, "error", Toast.LENGTH_SHORT);         
            return null;  
        } 
    }  

    public class Read extends AsyncTask<String, Integer, String> {      
        @Override     
        protected String doInBackground(String... params) {         
            // TODO Auto-generated method stub         
            try {                
                json = products(upc);
                return json.getString(params[0]);         
            } catch (ClientProtocolException e) {             
                    // TODO Auto-generated catch block             
                    e.printStackTrace();        
            } catch (IOException e) {             
                    // TODO Auto-generated catch block             
                    e.printStackTrace();         
            } catch (JSONException e) {             
                    // TODO Auto-generated catch block            
                    e.printStackTrace();         
            }         
            return null;     
        }  

    @Override 
    protected void onPostExecute(String result){     
    httpStuff.setText(result);
    } 
}

But I am not getting any text in httpStuff.

This is the logcat:

D/SntpClient(61): request time failed: java.net.SocketException: Address family not    supported by protocol
W/System.err(793): org.apache.http.conn.ConnectTimeoutException: Connect to /209.85.175.95:443 timed out
W/System.err(793):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
W/System.err(793):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
W/System.err(793):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err(793):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err(793):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
W/System.err(793):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(793):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(793):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(793):  at com.android.example.jsonexample.JSONExampleActivity.products(JSONExampleActivity.java:53)
W/System.err(793):  at com.android.example.jsonexample.JSONExampleActivity$Read.doInBackground(JSONExampleActivity.java:77)
W/System.err(793):  at com.android.example.jsonexample.JSONExampleActivity$Read.doInBackground(JSONExampleActivity.java:1)
W/System.err(793):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
W/System.err(793):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
W/System.err(793):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
W/System.err(793):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
W/System.err(793):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
W/System.err(793):  at java.lang.Thread.run(Thread.java:1019)
D/SntpClient(61): request time failed: java.net.SocketException: Address family not supported by protocol

Please help me find the reason why I am getting error.

like image 545
DSP Avatar asked Apr 06 '12 12:04

DSP


People also ask

How can I get product details from barcode?

To get product information you'll need to use some database of products that's searchable by UPC. A quick google search came up with a few suggestions, such as https://www.barcodelookup.com/api .

Do barcodes tell you the price?

Things To Know About Using UPC BarcodesUPCs Do Not Carry Prices – The UPC code simply identifies the product. When the UPC is scanned at the store, the retailer's computer POS system pulls up the most current price for that item.

How do you find the price of a barcode?

You will often find a Price Checker on a shelf of a store, like a supermarket, electronics shop or DIY shop. There may also be a screen with a Barcode Scanner below it, which you can use to scan a product's barcode. You hold the product near the Barcode Scanner and the relevant information is shown on the display.

Does barcode contain product name?

Barcode contains information about a product like; price & weight of the product, date of manufacturing and expiry, name of the manufacturer etc. Barcode is allocated by an international institution set up for this purpose. Every product has a unique barcode all over the world.


2 Answers

Try adding on your API id to the url. https://developers.google.com/shopping-search/v1/getting_started#getting-started

I tried it and was able to get the information of Michael Kors MK5412 Chronograph Watches based of the url of your code.

https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=691464717759&restrictBy=gtin=691464717759&key={your key here}

As a result, you have to fix your url builder to match ^.

Also make sure you put

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

in your manifest. Credit to this guy: http://androidforums.com/developer-101/100793-java-net-unknownhostexception.html.

Happy coding :)

like image 51
louis1204 Avatar answered Oct 05 '22 23:10

louis1204


You want to use the API and search by GTIN which is what the number encoded in the bar code represents.

like image 35
Shane Wealti Avatar answered Oct 06 '22 00:10

Shane Wealti