我如何做一个凌空的JSONObject请求与自定义对象作为参数?自定义、对象、参数、如何做一个

2023-09-05 08:39:50 作者:凉窗熄夏

我试图让使用凌空库到服务器,它有两个参数,一个对象(地址)和不同的对象(租户)的列表中的JSONObject POST请求

当我试图发出请求,第一个参数(地址)被凌空格式发送之前的请求不接受由服务器。

我的要求看起来是这样的:

  JsonObjectRequest jsonObjReq =新JsonObjectRequest(Method.POST,SERVER_URL,
    postPropertyJSONObject,responseListener,errorListener)
 

我的postPropertyJSONObject是这样创建的:

 的JSONObject的obj =新的JSONObject();
obj.put(地址,convertAddressToJson(property.getAddress())的toString());
obj.put(租户,prop.getTenantList());
 
JavaWeb Ajax异步请求 json对象 服务器如何返回json数据 使用ajax完成一个案例

在convertAddressToJson()方法是这样的:

 的JSONObject的JSONObject =新的JSONObject();
jsonObject.put(建,address.getBuilding());
jsonObject.put(街,address.getStreet());
jsonObject.put(镇,address.getTown());
jsonObject.put(ZIP code,address.getZip code());
jsonObject.put(县,address.getCounty());
jsonObject.put(国家,address.getCountry());
 

我想刚好路过的地址对象,但这是没有序列号在所有所以也没擦出火花。我也尝试只是路过Address.toString()在地址中的JSONObject的领域中使用的要求,但没有擦出火花。

我的财产对象的的getAddress()方法返回一个地址对象,它看起来是这样的:

 公共类地址{

    私人字符串街;
    私人字符串建设;
    私人字符串镇;
    私人字符串邮编code;
    私人字符串县;
    私人字符串的国家;
    // getter和setter
}
 

当我登录的地址之前,我通过它,它看起来像这样的要求:

  {城市:MyTown,街:MyStreet,县志:MyCounty,国家:MyCountry
 拉链code:MyZip code,楼:MyBuilding}
 

但是,当服务器日志它已收到,它看起来是这样的:

  {\城\:\MyTown \,\街\:\MyStreet \,\县\:\MyCounty \,
 \国家\:\MyCountry \,\邮编code \:\MyZip code \,\建筑\:\MyBuilding \}
 

由凌空应用这个格式似乎是改变我通过我的要求的值,因此谁能告诉我,如果有更好的方法来处理这​​个任务,应该是比较简单?我做了使用字符串和整数值请求到同一台服务器,但我有这个问题,同时试图通过一个自定义的类作为参数。

修改

使用wbelarmino的提示,我转而使用一个HashMap来存储我的自定义对象,并创建从一个JSONObject的:

 的HashMap<字符串地址> PARAMS =新的HashMap<字符串地址>();
params.put(地址,prop.getAddress());
requestObject =新的JSONObject(PARAMS);
 

解决方案

您可以试试这个:

 最后的字符串URL =/抽射/资源/ 12;
 

post数据被发送到服务器

 的HashMap<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
params.put(令牌,AbCdEfGh123456);

JsonObjectRequest REQ =新JsonObjectRequest(URL,新的JSONObject(PARAMS)
新Response.Listener<的JSONObject>(){
   @覆盖
   公共无效onResponse(JSONObject的响应){
       尝试 {
           VolleyLog.v(回应:%N%S,response.toString(4));
       }赶上(JSONException E){
           e.printStackTrace();
       }
   }
},新Response.ErrorListener(){
   @覆盖
   公共无效onErrorResponse(VolleyError错误){
       VolleyLog.e(错误,error.getMessage());
   }
});
 

请参阅在Android的usingvolley

I'm trying to make a JSONObject POST request using the Volley library to a server which takes 2 parameters, an object (Address) and a list of different objects (Tenants).

When I try to make the request, the first parameter (Address) is formatted by Volley before it is sent and the request is not accepted by the server.

My request looks something like this:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, SERVER_URL,
    postPropertyJSONObject, responseListener, errorListener)

My postPropertyJSONObject is created like this:

JSONObject obj = new JSONObject();
obj.put("Address", convertAddressToJson(property.getAddress()).toString());
obj.put("Tenants", prop.getTenantList());

The convertAddressToJson() method looks like this:

JSONObject jsonObject= new JSONObject();
jsonObject.put("Building", address.getBuilding());
jsonObject.put("Street", address.getStreet());
jsonObject.put("Town", address.getTown());
jsonObject.put("ZipCode", address.getZipCode());
jsonObject.put("County", address.getCounty());
jsonObject.put("Country", address.getCountry());

I tried just passing in the Address object but this wasn't serialized at all so it didn't work either. I also tried just passing Address.toString() in the "Address" field of the JSONObject used in the request but that didn't work either.

The getAddress() method of my Property object returns an Address object which looks something like this:

public class Address {

    private String Street;
    private String Building;
    private String Town;
    private String ZipCode;
    private String County;
    private String Country;
    // getters and setters
}

When I Log the address before I pass it the the request it looks like this:

{"Town":"MyTown","Street":"MyStreet","County":"MyCounty","Country":"MyCountry",
 "ZipCode":"MyZipCode","Building":"MyBuilding"}

But when the server Logs what it has received, it looks like this:

{\"Town\":\"MyTown\",\"Street\":\"MyStreet\",\"County\":\"MyCounty\",
 \"Country\":\"MyCountry\",\"ZipCode\":\"MyZipCode\",\"Building\":\"MyBuilding\"}"

This formatting applied by Volley seems to be altering the value I pass with my request so can anyone tell me if there's a better way to approach this task that should be relatively straightforward? I've made requests to the same server using String and Integer values but I've had this problem while trying to pass a custom class as parameter.

EDIT

Using wbelarmino's tip, I switched to using a hashmap to store my custom object and created a JSONObject from that:

HashMap<String, Address> params = new HashMap<String, Address>();
params.put("Address", prop.getAddress());
requestObject = new JSONObject(params);

解决方案

You can try this:

final String URL = "/volley/resource/12";

Post params to be sent to the server

HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "AbCdEfGh123456");

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
   @Override
   public void onResponse(JSONObject response) {
       try {
           VolleyLog.v("Response:%n %s", response.toString(4));
       } catch (JSONException e) {
           e.printStackTrace();
       }
   }
}, new Response.ErrorListener() {
   @Override
   public void onErrorResponse(VolleyError error) {
       VolleyLog.e("Error: ", error.getMessage());
   }
});

See more Http requests in android usingvolley