有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用条形码号获取产品信息,如名称、价格等

我正在开发一个应用程序,它使用barcode在扫描barcode之后获取物品的产品信息

我不希望用户单独安装ZXing barcode应用程序,所以我将ZXing代码嵌入到我的项目中。因此,我能够获得barcode ID number

我想通过谷歌搜索api获取产品信息,如名称、制造商、价格等

这是我使用的代码

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中的任何文本

这是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.安卓.example.jsonexample.JSONExampleActivity.products(JSONExampleActivity.java:53)
W/System.err(793):  at com.安卓.example.jsonexample.JSONExampleActivity$Read.doInBackground(JSONExampleActivity.java:77)
W/System.err(793):  at com.安卓.example.jsonexample.JSONExampleActivity$Read.doInBackground(JSONExampleActivity.java:1)
W/System.err(793):  at 安卓.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

请帮我找出我出错的原因


共 (2) 个答案

  1. # 1 楼答案

    您需要使用the API并按GTIN搜索,这是条形码中编码的数字所代表的

  2. # 2 楼答案

    尝试将API id添加到urlhttps://developers.google.com/shopping-search/v1/getting_started#getting-started

    我试过了,根据你代码的url,我得到了迈克尔·科尔斯MK5412计时手表的信息

    https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=691464717759&restrictBy=gtin=691464717759&key={你的钥匙在这里}

    因此,您必须修复url生成器以匹配^

    还要确保你把

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

    在你的舱单上。这家伙的功劳是:http://androidforums.com/developer-101/100793-java-net-unknownhostexception.html

    快乐编码:)