2016-06-16 59 views
0

我使用android volley向服務器端發送請求,另一方面在服務器端使用php。 這裏是我的Android代碼,以接收來自服務器的響應(什麼並不重要params爲,因爲我不需要在服務器端PARAMS呢!):android volley不接收php json對象

 JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params), 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        try { 
         Log.d("ATA d Response", response.toString()); 
         VolleyLog.v("ATA Response:%n %s", response.toString(4)); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.d("ATA Response", " RESPONSE ERROR"); 
      VolleyLog.e("Error: ", error.getMessage()); 
     } 
    }); 

    req.setShouldCache(false); 
    queue.getCache().clear(); 
    queue.add(req); 

現在,當在服務器端我返回的響應東西像這樣:

if(isset($_POST)) {  
    echo json_encode(array("x"=>"y", "m"=>"n")); 
    exit(); 
} 

這一切都是好的,響應是服務器發送的!

但是!在服務器端,當我回來的迴應是這樣的:

if(isset($_POST)) {  
    echo json_encode(array(array("0"=>"1"),array("2"=>"3"),array("4"=>"5"))); 
    exit(); 
} 

響應不正常,並得到這個錯誤:

06-17 02:28:53.456 D/Volley: [1296] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] MY_URL 0xa64d9c1b NORMAL 1> [lifetime=3645], [size=60], [rc=200], [retryCount=0] 

06-17 02:28:53.466 D/ATA Response: RESPONSE ERROR 

06-17 02:28:53.466 E/Volley: [1] 2.onErrorResponse: Error: 

這有什麼錯我的代碼?

+0

添加json格式您正在發送數組中的json並獲取對象可能使數組請求獲取json ... –

+0

@nEwDeV請澄清你說的,沒有得到! –

回答

0

嘗試發送內容類型標題。 application/json應該完成這項工作。

header("Content-type:application/json"); 

在產生任何輸出之前(即在echo聲明之前)發送報頭。

+0

雖然id添加了標題,但不起作用! –