问题
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.
回答1:
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 :)
回答2:
You want to use the API and search by GTIN which is what the number encoded in the bar code represents.
回答3:
There are also other barcode databases, like ean-search.org.
回答4:
I think,"a barcode number used to denote a particular product and the barcode number is unique for all over the world.most barcode apis find the given barcode number from their own database to get that particular product information (if given barcode number exists in their database) otherwise it does not give any result."
01.first you should create your own database including barcode numbers for each item in your database.(there are so many barcode types that you can use)
02.if you introduce a new product, you should get or buy a valid barcode number for your new product(make sure that your number is unique for all over the world).--
** then you can use your database to find the item information for a given barcode **
来源:https://stackoverflow.com/questions/10043579/getting-product-information-like-name-price-etc-using-barcode-number