本文介绍了如何使用自定义对象作为参数发出 Volley JSONObject 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Volley 库向服务器发出 JSONObject POST 请求,该请求带有 2 个参数、一个对象(地址)和一个不同对象(租户)的列表.

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).

当我尝试发出请求时,第一个参数(地址)在发送之前被 Volley 格式化,并且请求不被服务器接受.

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.

我的请求如下所示:

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

我的 postPropertyJSONObject 是这样创建的:

My postPropertyJSONObject is created like this:

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

convertAddressToJson() 方法如下所示:

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());

我尝试只传入 Address 对象,但这根本没有序列化,因此它也不起作用.我还尝试在请求中使用的 JSONObject 的地址"字段中传递 Address.toString() 但这也不起作用.

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.

我的 Property 对象的 getAddress() 方法返回一个 Address 对象,它看起来像这样:

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"}"

Volley 应用的这种格式似乎改变了我通过请求传递的值,所以有人能告诉我是否有更好的方法来处理这个应该相对简单的任务?我已经使用 String 和 Integer 值向同一台服务器发出请求,但是在尝试将自定义类作为参数传递时遇到了这个问题.

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.

编辑

使用 wbelarmino 的提示,我改用哈希图来存储我的自定义对象并从中创建了一个 JSONObject:

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);

推荐答案

你可以试试这个:

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());
   }
});

查看更多android usingvolley中的Http请求

这篇关于如何使用自定义对象作为参数发出 Volley JSONObject 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:17