Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...