2015-04-14 33 views
0

我試圖從使用Volley的URL檢索jsonarray。 的問題是,我得到Android Volley。 JsonArray JsonException在字符0處輸入結束

JsonException end of input at character 0 

代碼如下:

JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, openMatchesUrl, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d("JSON", response.toString()); 
         try { 
          for (int i = 0; i < response.length(); i++) { 

           //do stuff 
          } 

         } catch (JSONException e) { 
          e.printStackTrace(); 
          Toast.makeText(getApplicationContext(), 
            "Error: " + e.getMessage(), 
            Toast.LENGTH_LONG).show(); 
         } 

        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getApplicationContext(), 
           "onErrorResponse ongoing: "+error.getMessage(), Toast.LENGTH_SHORT).show(); 
        } 
       }){  
     @Override 
     protected Map<String, String> getParams() 
     { 
       //build params 
     } 
    }; 
    // Add the request to the RequestQueue. 
    queue.add(req); 

我想這個問題是在錯誤的參數。但我嘗試了一個簡單的字符串請求:

StringRequest req = new StringRequest(Request.Method.POST, openMatchesUrl, 
       new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
       Log.d("JSON", "resp: " +response); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.d("JSON", error.toString()); 
     } 
    }){  
      @Override 
      protected Map<String, String> getParams() 
      { 
        //build params 
      } 
     }; 

它實際上正確地返回json。例如:

[{"roundid":4152,"numberofplayers":1,"dateevent":"2015-04-13 19:45:32.121124+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":415‌​4,"numberofplayers":1,"dateevent":"2015-04-13 20:16:08.845409+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":415‌​5,"numberofplayers":1,"dateevent":"2015-04-13 20:18:22.002411+02","playernames":"cat","turn":1,"codedboard":""}] 

這裏有什麼問題?

+1

請分享的'Log.d輸出( 「JSON」, 「RESP:」 +響應);',並且是指http://stackoverflow.com/questions/8740381/getting-a-jsonexception -in-input-at-character-0 –

+0

這裏是輸出: 'resp:[{「roundid」:4152,「numberofplayers」:1,「dateevent」:「 2015-04-13 19:45:32.121124 + 02「,」playernames「:」cat「,」turn「:1,」codedboard「:」「},{」roundid「:4154,」numberofplayers「:1,玩家姓名「:」貓「,」轉「:1,」編碼板「:」「},{」roundid「:4155,」numberofplayers「 :1,「dateevent」:「2015-04-13 20:18:22.002411 + 02」,「playernames」:「cat」,「turn」:1,「codedboard」:「」}]' –

回答

0

我終於解決了這個問題,這裏的問題是由於某種原因JSonArrayRequest沒有采取POST參數。

所以我只手動所附URL參數的

0

在黑暗中拍攝的總數,但是我在RSS解析器中發生了類似的情況。事實證明,我使用的URL是HTTP,但重定向到HTTPS,我使用的是HttpURLConnection而不是HttpsURLConnection。

雖然我沒有使用Android Volley,所以YMMV。

相關問題