【发布时间】:2022-01-10 06:26:46
【问题描述】:
我创建了图片中的json文件之类的函数。当我发送一个发布请求时,响应是“意外响应代码 500”。我认为问题是由 json 数组引起的,我不明白为什么会出现这个错误。服务器 url 工作正常,在 swift 上成功发布。谢谢
private void CreditCardParams(Map<String, String> requestParameters) {
JSONArray creditCardItems = new JSONArray();
JSONObject item_creditCardItems = new JSONObject();
try {
item_creditCardItems.put("CC_NUMBER", "1111");
item_creditCardItems.put("EXP_MONTH", “11”);
item_creditCardItems.put("EXP_YEAR", “1111");
item_creditCardItems.put("CC_CVV", “111”);
item_creditCardItems.put("CC_OWNER", “TEST USER”);
item_creditCardItems.put("INSTALLMENT_NUMBER", "1");
} catch (JSONException e) {
e.printStackTrace();
}
creditCardItems.put(item_creditCardItems);
requestParameters.put("CreditCard", (creditCardItems.toString()));
}
private void CustomerParams(Map<String, String> requestParameters) {
JSONArray CustomerItems = new JSONArray();
JSONObject item_Customer = new JSONObject();
try {
item_Customer.put("FIRST_NAME", "Firstname");
item_Customer.put("LAST_NAME", "Lastname");
item_Customer.put("MAIL", "test@test.com");
item_Customer.put("PHONE", "+11111111");
item_Customer.put("CITY", "Test");
item_Customer.put("STATE", "Test");
item_Customer.put("ADDRESS", "Test");
item_Customer.put("CLIENT_IP", "111.11.11.11”);
} catch (JSONException e) {
e.printStackTrace();
}
CustomerItems.put(item_Customer);
requestParameters.put("Customer", (CustomerItems.toString()));
}
private void ProductParams(Map<String, String> requestParameters) {
JSONArray ProductItems = new JSONArray();
JSONObject item_Product = new JSONObject();
try {
item_Product.put("PRODUCT_ID", "1");
item_Product.put("PRODUCT_NAME", “1”);
item_Product.put("PRODUCT_CATEGORY", “Elec.”);
item_Product.put("PRODUCT_DESCRIPTION", “Info”);
item_Product.put("PRODUCT_AMOUNT", 15.00);
} catch (JSONException e) {
e.printStackTrace();
}
ProductItems.put(item_Product);
requestParameters.put("Product", (ProductItems.toString()));
}
private void ConfigParams(Map<String, String> requestParameters) {
JSONArray configItems = new JSONArray();
JSONObject item = new JSONObject();
try {
item.put("MERCHANT", "TEST1234");
item.put("MERCHANT_KEY", “123”);
item.put("BACK_URL", "localhost");
item.put("PRICES_CURRENCY", "USD");
item.put("ORDER_REF_NUMBER", "RFN0001");
item.put("ORDER_AMOUNT", 15.00);
} catch (JSONException e) {
e.printStackTrace();
}
configItems.put(item);
requestParameters.put("Config", (configItems.toString()));
}
private void post_func(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
}
}
){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String,String>();
CreditCardParams(params);
CustomerParams(params);
ProductParams(params);
ConfigParams(params);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
【问题讨论】:
-
回答为 500 表示服务器中发生了意外情况。要了解出了什么问题,我们需要更多信息。这个请求对邮递员有效吗?服务器对数据类型的期望是什么?
-
我将我的参数函数(CreditCardParams、ConfigParams...)转换为新的json参数,响应成功。但我不明白这个解决方案。新的 json 参数=> jsonObject = new JSONObject("{\n" + " \"Configuration\" : {\n" + " \"SELLER\" : \"TEST1234\",\n" + ... }} )
标签: java android post android-volley