Android Google-Shopping API force closes while parsing

Posted by Sam Jackson on Stack Overflow See other posts from Stack Overflow or by Sam Jackson
Published on 2012-03-31T20:40:45Z Indexed on 2012/04/01 11:28 UTC
Read the original article Hit count: 289

I'm trying to send a request to the Google-Shopping API with the following static method which I think is working:

public static String GET_TITLE(String url) throws JSONException {

    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    // http post
    try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    } catch(Exception e) {
            Log.e("log_tag", "Error in http connection "+e.toString());
    }

The URL I'm passing is this BTW: https://www.googleapis.com/shopping/search/v1/public/products/country=US&q=shirts&alt=json &rankBy=relevancy&key=AIzaSyDRKgGmJrdG6pV6DIg2m-nmIbXydxvpjww

Next I try to parse this response (where I think the problem comes in) in the same method:

    try {
        jArray = new JSONObject(result);            
    } catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    JSONObject itemObject = jArray.getJSONObject("items");
    JSONObject productObject = itemObject.getJSONObject("product");

    String attributeGoogleId = productObject.getString("googleId");
    String attributeProviderId = productObject.getString("providerId");
    String attributeTitle = productObject.getString("title");*/
    String attributePrice = productObject.getString("price");
    JSONObject popupObject = productObject.getJSONObject("popup");

        return attributeTitle;
    }

This has been so frustrating, I know it should be simple but everywhere I look I just can't quite get it to work, I'm not exactly sure what the error is since I'm testing it on my HTC Desire because my emulator gives an 'invalid command-line parameter' error when starting, but that's a different issue, anyway, thanks in advance!

EDIT:

The first one makes it look like there's a problem with the URL, should I change it and see if it makes a difference?

04-01 12:09:05.142: ERROR/log_tag(24968): Error in http connection java.net.UnknownHostException: www.googleapis.com
04-01 12:09:05.142: ERROR/log_tag(24968): Error converting result java.lang.NullPointerException
04-01 12:09:05.142: ERROR/log_tag(24968): Error parsing data org.json.JSONException: End of input at character 0 of 
04-01 12:09:05.142: DEBUG/AndroidRuntime(24968): Shutting down VM
04-01 12:09:05.142: WARN/dalvikvm(24968): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): FATAL EXCEPTION: main
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent {     act=com.google.zxing.client.android.SCAN flg=0x80000 (has extras) }} to activity     {com.spectrum.stock/com.spectrum.stock.CaptureActivity}: java.lang.NullPointerException
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.ActivityThread.deliverResults(ActivityThread.java:3734)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.ActivityThread.access$2800(ActivityThread.java:135)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.os.Looper.loop(Looper.java:144)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at dalvik.system.NativeStart.main(Native Method)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): Caused by: java.lang.NullPointerException
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at com.spectrum.stock.JSONResponse.GET_TITLE(JSONResponse.java:61)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at com.spectrum.stock.CaptureActivity.onActivityResult(CaptureActivity.java:78)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.Activity.dispatchActivityResult(Activity.java:3931)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
04-01 12:09:05.152: ERROR/AndroidRuntime(24968): ... 11 more
04-01 12:09:05.162: WARN/ActivityManager(96):    Force finishing activity com.spectrum.stock/.CaptureActivity

© Stack Overflow or respective owner

Related posts about android

Related posts about parsing