如姓名获取产品信息,价格等用吧code号 [英] Getting Product information like name, price etc using barcode number

查看:85
本文介绍了如姓名获取产品信息,价格等用吧code号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个应用程序,它使用吧code扫描吧code后得到物品的产品信息。

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

我不希望用户安装ZXing吧code应用程序分开,所以我嵌入式的ZXing code到我的项目。所以,我能够得到吧code ID号。

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.

我想用用谷歌搜索API购物吧code号得到这样的名称,生产厂家,价格等方面的产品信息。

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

下面是code我已经用

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); } 


} 

}

但我没有得到在httpStuff任何文本。

But I am not getting any text in httpStuff.

这是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.

推荐答案

试着增加你的API ID到URL。的https://developers.google.com/shopping-search/v1/getting_started#getting-started

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

我试了一下,并能得到Michael Kors的MK5412计时腕表根据您的code中的URL的信息。

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

<一个href="https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=691464717759&restrictBy=gtin=691464717759&key=" rel="nofollow">https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=691464717759&restrictBy=gtin=691464717759&key={your这里的关键}

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

另外,还要确保你把

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

在你的清单。感谢这家伙:的http://androidforums.com/developer-101/100793-java-net-unknownhostexception.html.

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

编码快乐:)

这篇关于如姓名获取产品信息,价格等用吧code号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆