0

Ich versuche, eine Anfrage an den Google-Shopping-API mit der folgenden statischen Methode zu senden, die ich denke, arbeiten:Android Google-Shopping-API Kraft schließt beim Parsen

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

Die URL Ich bin vorbei BTW, ist dies: https://www.googleapis.com/shopping/search/v1/public/products/country=US&q=shirts&alt=json & rankBy = Relevanz & key = AIzaSyDRKgGmJrdG6pV6DIg2m-nmIbXydxvpjww

Next ich versuche, diese Antwort zu analysieren (wo ich denke, das Problem kommt in) in der gleichen Methode:

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

Das war so frustrierend, ich weiß, dass es einfach sein sollte, aber überall, wo ich hinschaue, kann ich es nicht funktionieren, ich bin mir nicht sicher, was der Fehler ist, seit ich es auf meinem HTC getestet habe Desire, weil mein Emulator beim Start einen 'ungültigen Befehlszeilenparameter' Fehler gibt, aber das ist ein anderes Problem, danke, im Voraus!

EDIT:

Der erst man es so aussehen läßt ein Problem mit der URL da ist, soll ich es ändern und sehen, ob es einen Unterschied macht?

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 

EDIT: Fehlerprotokoll von LogCat nach Änderungen vorgenommen.

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 !!! 
+0

können Sie das Fehlerprotokoll und die Antwort zur Verfügung stellen – Ishu

+0

Ich habe meinen Beitrag bearbeitet, um das Fehlerprotokoll aus dem LogCat –

+0

zu enthalten. Im obigen ist der Ergebniswert "", da der "Inputstream is" nicht in String konvertiert wird. – Ishu

Antwort

0

fand ich die Antwort auf diese Frage elsewhere, ich glaube, es war nur eine kleine Sache, wie es nach dem Wechsel nur ein paar Dinge gearbeitet, die die Antwort ist null, wie die Anforderung erfolgreich gesendet wurde gestoppt.

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


    } 
+0

Hallo danke für Ihre Hilfe, leider die Zeilen "Constants.TEXT_NETWORKERROR = Constants.MSG_ERROR_CONNECTION" und "Constants.TEXT_NETWORKERROR = Constants.MSG_ERROR_CONNECTION" geben jedoch "kann nicht aufgelöst werden" Fehler nachdem ich sie kommentiert habe, bekomme ich das gleiche Ergebnis (force close). Auch habe ich bemerkt, dass der erste Fehler in der LogCat ist "Fehler in HTTP-Verbindung", die in der Fangklausel für die erste try Klammer ist, was bedeutet, dass es richtig scheitert? Ich habe deine Methode direkt nach der ersten Fangklausel aufgerufen, aber es scheint, als ob das Problem früher auftritt, habe ich es an der falschen Stelle benutzt? –

+0

Fertig! Dieser Fehler tauchte wieder auf - "Fehler in HTTP-Verbindung java.net.UnknownHostException: www.googleapis.com" –

+0

Ihr Code ist richtig, aber Sie haben Handle Ausnahme in Ihrem HTTPpost – Ishu

Verwandte Themen