我尝试使用Volley库向服务器发出JSONObject POST请求,该服务器接受2个参数,一个对象(地址)和一个不同对象列表(租户)。
当我尝试发出请求时,第一个参数(地址)在发送之前由Volley格式化,服务器不接受该请求。
我的请求看起来像这样:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, SERVER_URL,
postPropertyJSONObject, responseListener, errorListener)
我的postPropertyJSONObject是这样创建的:
JSONObject obj = new JSONObject();
obj.put("Address", convertAddressToJson(property.getAddress()).toString());
obj.put("Tenants", prop.getTenantList());
convertAddressToJson()方法如下所示:
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对象,但这根本没有序列化,所以它也没有工作。我也试过在"地址"中传递Address.toString()。请求中使用的JSONObject字段,但它也没有工作。
我的Property对象的getAddress()方法返回一个如下所示的Address对象:
public class Address {
private String Street;
private String Building;
private String Town;
private String ZipCode;
private String County;
private String Country;
// getters and setters
}
当我在传递地址之前记录地址时,请求看起来像这样:
{"Town":"MyTown","Street":"MyStreet","County":"MyCounty","Country":"MyCountry",
"ZipCode":"MyZipCode","Building":"MyBuilding"}
但是当服务器记录它收到的内容时,它看起来像这样:
{\"Town\":\"MyTown\",\"Street\":\"MyStreet\",\"County\":\"MyCounty\",
\"Country\":\"MyCountry\",\"ZipCode\":\"MyZipCode\",\"Building\":\"MyBuilding\"}"
Volley应用的这种格式似乎正在改变我通过我的请求传递的价值所以任何人都可以告诉我,如果有更好的方法来处理这个应该相对简单的任务吗?我使用String和Integer值向同一服务器发出了请求,但在尝试将自定义类作为参数传递时,我遇到了这个问题。
修改
使用wbelarmino的提示,我转而使用hashmap存储我的自定义对象并从中创建了一个JSONObject:
HashMap<String, Address> params = new HashMap<String, Address>();
params.put("Address", prop.getAddress());
requestObject = new JSONObject(params);
答案 0 :(得分:17)
你可以试试这个:
final String URL = "/volley/resource/12";
将params发送到服务器
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());
}
});
答案 1 :(得分:0)
final RequestQueue requestQueue = Volley.newRequestQueue(this);
final String url ="http://mykulwstc000006.kul/Services/Customer/Register";
Map<String, String> params = new HashMap<String, String>();
params.put("MobileNumber", "+97333765439");
params.put("EmailAddress", "danish.hussain4@das.com");
params.put("FirstName", "Danish2");
params.put("LastName", "Hussain2");
params.put("Country", "BH");
params.put("Language", "EN");
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());
}
}){
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
String username ="eli@gmail.com";
String password = "elie73";
String auth =new String(username + ":" + password);
byte[] data = auth.getBytes();
String base64 = Base64.encodeToString(data, Base64.NO_WRAP);
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization","Basic "+base64);
return headers;
}
};
requestQueue.add(req);
}