2017-01-03 66 views
0

我想提出一個凌空請求,並嘗試後一個整數,Send Int in HashMap with Volley我最後的代碼如下所示以下這個解決方案:凌空POST整數

 JSONArray jsonArray = new JSONArray(); 
    JSONObject jsonObject = new JSONObject(); 

    try{ 
     jsonObject.put("store",storeId); 
     jsonObject.put("people", people); 
     jsonObject.put("date",reserveDate); 
     jsonObject.put("time", reserveTime); 
     jsonArray.put(jsonObject); 

     Log.i("jsonString", jsonObject.toString()); 


    }catch(Exception e){ 

    } 


    StringRequest stringRequest = new StringRequest(Request.Method.POST, domain + api, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 

        Log.e("Response", response); 

        return; 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        Log.e("Error.Response", error.toString()); 
        String json = null; 

        NetworkResponse response = error.networkResponse; 
        if(response != null && response.data != null){ 
         switch(response.statusCode){ 
          case 400: 
           json = new String(response.data); 
           System.out.println(json); 
           //json = trimMessage(json, "message"); 
           //if(json != null) displayMessage(json); 
           break; 
         } 
         //Additional cases 
        } 

       } 

      }) 

    { 
     @Override 
     protected Map<String,String> getParams(){ 
      Map<String,String> params = new HashMap<String, String>(); 
      params.put("store",storeId); 
      params.put("people",String.valueOf(people)); 
      params.put("date", "2017-01-04"); 
      params.put("time", reserveTime); 
      Log.d("params", params.toString()); 
      return params; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<>(); 
      headers.put("Authorization", finalToken); 
      Log.d("headers", headers.toString()); 
      return headers; 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

在這裏,我要發佈「人」作爲一個整數,但錯誤消息我得到的是「人」是一個字符串值,我宣佈「人」爲:

Integer people = Integer.parseInt(reserveNum); 

其中reserveNum是EditText上一個數值。我如何讓我的請求中的「peopel」是一個整數?

UPDATE 錯誤信息是:

{"errors":[{"message":"Invalid type: string (expected integer)","params":{"type":"string","expected":"integer"},"code":0,"dataPath":"/people","schemaPath":"/properties/people/type",..... 
+0

String.valueOf(people)is this line gives u error? – rafsanahmad007

+0

@ rafsanahmad007我不知道是否它的確切線,但這將是我的猜測,也試圖將String.valueOf(人)更改爲String.valueOf(3)進行測試並沒有幫助 – JerryKo

+0

您嘗試了整數。 toString(people) – rafsanahmad007

回答

1

我已通過改變Stringrequest到的JSONObject reqeust想通該溶液中。通過這樣做,我的請求將採用包含整數而不是字符串的JSON格式。

JSONArray jsonArray = new JSONArray(); 
    JSONObject jsonObject = new JSONObject(); 

    try{ 
     jsonObject.put("store",storeId); 
     jsonObject.put("people", people); 
     jsonObject.put("date",reserveDay); 
     jsonObject.put("time", reserveTime); 
     jsonArray.put(jsonObject); 
     Log.i("jsonString", jsonObject.toString()); 


    }catch(Exception e){ 

    } 

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, domain + api, jsonObject, 
      new Response.Listener<JSONObject>(){ 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.e("Response", response.toString()); 
        try { 
         JSONArray arrData = response.getJSONArray("data"); 
         parseData(arrData); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener(){ 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("Error.Response", error.toString()); 
        String json = null; 
        NetworkResponse response = error.networkResponse; 
        if(response != null && response.data != null){ 
         switch(response.statusCode){ 
          case 400: 

           json = new String(response.data); 
           System.out.println(json); 
           break; 
         } 
         //Additional cases 
        } 
       } 
      }) 
    { 
     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<>(); 
      headers.put("Authorization", finalToken); 
     Log.d("headers", headers.toString()); 
      return headers; 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(jsonObjectRequest); 
} 
0

嘗試下面的代碼替換上面的代碼:

JSONArray jsonArray = new JSONArray(); 
    JSONObject jsonObject = new JSONObject(); 

    try{ 
     jsonObject.put("store",storeId); 
     //people is a integer 
     jsonObject.put("people", people); 
     jsonObject.put("date",reserveDate); 
     jsonObject.put("time", reserveTime); 
     jsonArray.put(jsonObject); 

     Log.i("jsonString", jsonObject.toString()); 


    }catch(Exception e){ 

    } 


    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST,domain + api, jsonObject, 
       new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(String response) { 

        Log.e("Response", response); 

        return; 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

        Log.e("Error.Response", error.toString()); 
        String json = null; 

        NetworkResponse response = error.networkResponse; 
        if(response != null && response.data != null){ 
         switch(response.statusCode){ 
          case 400: 
           json = new String(response.data); 
           System.out.println(json); 
           //json = trimMessage(json, "message"); 
           //if(json != null) displayMessage(json); 
           break; 
         } 
         //Additional cases 
        } 

       } 

      }) 

    { 
     @Override 
     protected Map<String,String> getParams(){ 
      Map<String,String> params = new HashMap<String, String>(); 
      params.put("store",storeId); 
      params.put("people",String.valueOf(people)); 
      params.put("date", "2017-01-04"); 
      params.put("time", reserveTime); 
      Log.d("params", params.toString()); 
      return params; 
     } 

     @Override 
     public Map<String, String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers = new HashMap<>(); 
      headers.put("Authorization", finalToken); 
      Log.d("headers", headers.toString()); 
      return headers; 
     } 
    }; 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 
+0

感謝您的回覆,但是我仍然得到錯誤,人是一個字符串類型,需要整數 – JerryKo

+0

檢查您的服務器API代碼...無論它接受字符串或整數... – rafsanahmad007