2016-08-03 43 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应用程序使用这个方法。 –