Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Google购物API强制解析时关闭_Android_Json_Parsing_Google Api Java Client_Google Shopping Api - Fatal编程技术网

Android Google购物API强制解析时关闭

Android Google购物API强制解析时关闭,android,json,parsing,google-api-java-client,google-shopping-api,Android,Json,Parsing,Google Api Java Client,Google Shopping Api,我正在尝试使用以下静态方法向Google Shopping API发送请求,我认为这是可行的: public static String GET_TITLE(String url) throws JSONException { InputStream is = null; String result = ""; JSONObject jArray = null; // http post try { HttpClient htt

我正在尝试使用以下静态方法向Google Shopping API发送请求,我认为这是可行的:

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());
    }
我要传递的URL是这样的,顺便说一句:&rankBy=relevance&key=AIzaSyDRKgGmJrdG6pV6DIg2m-nmIbXydxvpjww

接下来,我尝试分析这个响应,我认为问题来自同一个方法:

    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;
    }
这是如此令人沮丧,我知道它应该很简单,但无论我在哪里看,我都不能完全让它工作,我不确定错误是什么,因为我在我的HTC Desire上测试它,因为我的模拟器在启动时给出了一个“无效的命令行参数”错误,但这是一个不同的问题,无论如何,提前感谢

编辑:

第一个让它看起来像是URL有问题,我应该改变它,看看它是否有区别吗

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
编辑:更改后来自LogCat的错误日志

04-01 15:00:33.912: ERROR/QualcommCameraHardware(67): native_access_parm: error (No such file or directory): fd 30, type 1, length 32, status 0
04-01 15:00:34.002: ERROR/QualcommCameraHardware(67): num_buffers = 4
04-01 15:00:34.002: ERROR/QualcommCameraHardware(67): num_buffers = 8
04-01 15:00:34.082: ERROR/mm-camera 8x vfe(67): vfe_util_updaterollofftbl: sensor doesn't support rolloff correction by VFE
04-01 15:00:35.022: ERROR/mm-camera-af(67): af_algo_preview: FAILED....
04-01 15:00:37.872: ERROR/mm-camera-af(67): af_algo_preview: FAILED....
04-01 15:00:38.052: ERROR/mm-camera 8x-vfe(67): Received VFE Stop ACK!!! ctrlCmd is NULL
04-01 15:00:38.482: ERROR/QualcommCameraHardware(67): native_access_parm: error (No such file or directory): fd 30, type 1, length 32, status 0
04-01 15:00:38.542: ERROR/QualcommCameraHardware(67): num_buffers = 4
04-01 15:00:38.542: ERROR/QualcommCameraHardware(67): num_buffers = 8
04-01 15:00:38.602: ERROR/mm-camera 8x vfe(67): vfe_util_updaterollofftbl: sensor doesn't support rolloff correction by VFE
04-01 15:00:39.182: ERROR/mm-camera-af(67): af_algo_preview: FAILED....
04-01 15:00:41.662: ERROR/mm-camera-af(67): af_algo_preview: FAILED....
04-01 15:00:44.742: ERROR/mm-camera-af(67): af_algo_preview: FAILED....
04-01 15:00:47.422: ERROR/ImapServer(351): catch exception
04-01 15:00:47.422: ERROR/ImapServer(351): java.lang.NumberFormatException: unable to parse 'BASE64' as integer
04-01 15:00:47.422: ERROR/ImapServer(351): at java.lang.Integer.parse(Integer.java:433)
04-01 15:00:47.422: ERROR/ImapServer(351): at java.lang.Integer.parseInt(Integer.java:422)
04-01 15:00:47.422: ERROR/ImapServer(351): at java.lang.Integer.parseInt(Integer.java:382)
04-01 15:00:47.422: ERROR/ImapServer(351): at java.lang.Integer.valueOf(Integer.java:682)
04-01 15:00:47.422: ERROR/ImapServer(351): at com.htc.android.mail.server.ImapServer.getTheDisplaySize(ImapServer.java:2810)
04-01 15:00:47.422: ERROR/ImapServer(351): at com.htc.android.mail.server.ImapServer.processFetchInfo(ImapServer.java:2694)
04-01 15:00:47.422: ERROR/ImapServer(351): at com.htc.android.mail.server.ImapServer.fetchInfo(ImapServer.java:2531)
04-01 15:00:47.422: ERROR/ImapServer(351): at com.htc.android.mail.server.ImapServer.refresh(ImapServer.java:1348)
04-01 15:00:47.422: ERROR/ImapServer(351): at com.htc.android.mail.RequestController$MainRequestQueueThread.run(RequestController.java:570)
04-01 15:00:47.622: ERROR/log_tag(26112): Error in http connection java.net.UnknownHostException: www.googleapis.com
04-01 15:00:47.632: ERROR/log_tag(26112): Error parsing data org.json.JSONException: End of input at character 0 of 
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): FATAL EXCEPTION: main
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): 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 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.ActivityThread.deliverResults(ActivityThread.java:3734)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.ActivityThread.access$2800(ActivityThread.java:135)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.os.Looper.loop(Looper.java:144)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.ActivityThread.main(ActivityThread.java:4937)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at java.lang.reflect.Method.invokeNative(Native Method)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at java.lang.reflect.Method.invoke(Method.java:521)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at dalvik.system.NativeStart.main(Native Method)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): Caused by: java.lang.NullPointerException
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at com.spectrum.stock.JSONResponse.GET_TITLE(JSONResponse.java:63)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at com.spectrum.stock.CaptureActivity.onActivityResult(CaptureActivity.java:78)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.Activity.dispatchActivityResult(Activity.java:3931)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
04-01 15:00:47.642: ERROR/AndroidRuntime(26112):     ... 11 more
04-01 15:00:48.272: ERROR/JavaBinder(96): !!! FAILED BINDER TRANSACTION !!!
04-01 15:00:48.282: ERROR/JavaBinder(96): !!! FAILED BINDER TRANSACTION !!!
04-01 15:00:48.282: ERROR/JavaBinder(96): !!! FAILED BINDER TRANSACTION !!!
04-01 15:00:48.282: ERROR/JavaBinder(96): !!! FAILED BINDER TRANSACTION !!!
04-01 15:00:48.282: ERROR/JavaBinder(96): !!! FAILED BINDER TRANSACTION !!!

我找到了这个问题的答案,我认为这只是一件小事,因为它在成功发送请求时,仅更改了几项使响应不为空的内容。你能提供错误日志和响应吗?我编辑了我的帖子,将日志中的错误日志包括在上面,结果值是因为输入流未被删除已转换为字符串。您用于获取数据的确切url是什么HI感谢您的帮助,不幸的是,Constants.TEXT\u NETWORKERROR=Constants.MSG\u ERROR\u CONNECTION和Constants.TEXT\u NETWORKERROR=Constants.MSG\u ERROR\u CONNECTION这两行给出了“无法解决”的错误,但是在注释掉它们之后,我得到了相同的结果强制关闭。我还注意到LogCat中的第一个错误是'errorinHTTP connection',它位于第一个try括号的catch子句中,这意味着它失败了,对吗?我在第一个catch子句之后调用了您的方法,但问题似乎来得更早,我是否在错误的位置使用了它?完成!这个错误再次出现-http连接java.net.UnknownHostException中的错误:www.googleapis.com您的代码是正确的,但是您的HttpPost中有句柄异常。我想我已经用catch括号处理了,我还能做什么?还是我应该把它完全取出来?对不起,我不明白你的意思。你能再解释一下吗?我该怎么编辑它?我应该把它拿出来换掉,还是干脆把它全拆了?
In the above the result value is "" since the "Inputstream is" is not being converted to String. to convert it to string use following method and try


    public static String convertStreamToString(InputStream is) {
            StringBuilder sb = new StringBuilder();
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                is.close();
            } catch(OutOfMemoryError om){
                //Constants.TEXT_NETWORKERROR = Constants.MSG_ERROR_CONNECTION;
                om.printStackTrace();
            } catch(Exception ex){
                //Constants.TEXT_NETWORKERROR = Constants.MSG_ERROR_CONNECTION;
                ex.printStackTrace();
            }
            return sb.toString();
        }//convertStreamToString()


Minor changes in your code to handle NullpointerException

try {
        jArray = new JSONObject(result);  
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;          
    } catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
return null;
    }


    }