2016-08-03 59 views
-2

在我的項目中,我想發送一個請求與地圖內的排球地圖。
但我收到錯誤,406錯誤。排球內地圖地圖JSON請求

這裏是我的要求:

StringRequest myReq = new StringRequest(Method.POST, METHOD_ACCOUNT_LOGIN, 
       new Response.Listener<String>() { 

        @Override 
        public void onResponse(String response) { 


         JSONObject veri_json; 
         try { 
          veri_json = new JSONObject(response); 


         } catch (JSONException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 

        } 
       }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       System.out.println(error.getMessage()); 
      } 
     }) { 


      protected Map<String, String> getParams() 
        throws com.android.volley.AuthFailureError { 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("user", generalParams); 

       return params; 
      }; 
     }; 

     App.getInstance().addToRequestQueue(myReq); 
    } 

,但我想這裏面的代碼導入

params.put("user",generalParams) 

這些行:

generalParams.put("name",notificationName); 
generalParams.put("notificationId", notificationID); 
generalParams.put("phoneNumber", phoneNumber); 
generalParams.put("regionType", regionType); 
generalParams.put("countryCode", countryCode); 
generalParams.put("platform", platform); 

這裏是我想送什麼:

"user":{ 
    "name":"John", 
    "notificationId":"id", 
    "phoneNumber":"number", 
    "regionType":"en", 
    "countryCode": "+10", 
    "platform":」ios」 
} 

但是,我不知道如何處理這個問題。

回答

0

首先,406意味着您發送的數據不正確,或者服務器不接受在該地址發送給它的數據。如果你有信心服務器不接受JSON數據,然後繼續前進......

爲了創建一個JSONObject這樣{ user: {} },你需要把一個JSONObject"user"關鍵,不明確的HashMap,因爲它會toString它。

因此,而是不喜歡這樣

protected Map<String, String> getParams() { 
    // Outer object 
    Map<String, String> params = new HashMap<String, String>(); 

    // Inner object 
    Map<String, String> userParams = new HashMap<String, String>(); 
    userParams.put("name",notificationName); 
    userParams.put("notificationId", notificationID); 
    userParams.put("phoneNumber", phoneNumber); 
    userParams.put("regionType", regionType); 
    userParams.put("countryCode", countryCode); 
    userParams.put("platform", platform); 

    JSONObject userJSON = new JSONObject(userParams); 

    // Nest the object at "user" 
    params.put("user", userJSON.toString()); 

    return params; 
} 

旁白:

就個人而言,我會建議你改變了服務器以這種形式來接受用戶因爲該鍵值似乎無關緊要,除非服務器對該鍵進行字符串類型檢查,這意味着您應該理想地重新構造API。

{ 
    "name":"John", 
    "notificationId":"id", 
    etc... 
} 
+0

感謝您的回答。但我真的是新的這些請求。那麼我可以在哪裏準確寫出這些代碼呢?這是在@Override受保護的Map getParams()? –

+0

啊,是的。對於那個很抱歉。 –

+0

這仍然是BasicNetwork.performRequest:意外的響應代碼406 :(實際上這個服務器接受這個,因爲ios應用程序使用這個方法。 –