gpt4 book ai didi

java - 截击 POST 整数

转载 作者:行者123 更新时间:2023-12-01 09:03:56 26 4
gpt4 key购买 nike

我正在发出 Volley 请求并尝试按照此解决方案 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);
}

这里我想将“people”作为整数发布,但我得到的错误消息是“people”是一个字符串值,我已将“people”声明为:

 Integer people = Integer.parseInt(reserveNum);

其中reserveNum是来自edittext的数值。如何使我的请求中的“peopel”成为整数?

更新错误消息是:

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

最佳答案

我通过将 Stringrequest 更改为 JSON 对象请求找到了解决方案。通过这样做,我的请求将采用包含整数而不是字符串的 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);
}

关于java - 截击 POST 整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41439693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com