问题描述
我需要调用一个将字符串数组作为POST参数的api.因此,以API定义的示例为例:
I need to call an api that expects a string array as a POST parameter. So for an example of an API definition:
POST api/names
预期的POST参数是名称和其他一些属性的数组,如下所示:
POST parameter expected is an array of names and some other attributes like below:
{ names: [ "John", "Bill" ], department: "Engineering" }
我目前正在使用Android文档中所述的自定义Volley框架,但似乎Volley的参数只能作为Map(字符串,键和值的字符串)传递.我已经有来自Android应用程序的数组,准备将其作为post参数传递,但是此Volley需要一个String.
I am currently using a custom Volley framework as described in the Android documentation but it seems like the parameters from Volley can only be passed as a Map of (String, String as key and value). I already have the array from my Android app, ready to be passed as a post parameter but this Volley expects a String.
我尝试使用Arrays.toString(myStringArray)转换数组,并像下面那样传递它,但是它不起作用.
I tried to convert my array using Arrays.toString(myStringArray) and passed it like below but it does not work.
String[] namesArray = new String[1];
namesArray[0] = "Bill";
Map<String, String> mapParams = new HashMap<String, String>();
mapParams.put("department", "Computer Science");
mapParams.put("names", Arrays.toString(namesArray));
// Then call the Volley here passing the mapParams.
当我只能使用Volley中的字符串时,如何调用需要数组字符串的api?
How can I call the api that expects a String of array when I can only use a String from Volley?
推荐答案
我将为您提供完整的代码,以通过POST方法将JsonObject发布到排球上.
I will give you full code to post JsonObject on volley, through POST method.
JSONObject js = new JSONObject();
try {
js.put("genderType", "MALE");
}
} catch (JSONException e) {
e.printStackTrace();
}
String url = "LINK TO POST/";
// Make request for JSONObject
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
Request.Method.POST, url, js,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e(TAG, "Response_Code from Volley" + "\n" + response.toString() + " i am king");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e(TAG, "Error: " + error.getMessage());
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
Log.e(TAG, "onErrorResponse: of uploadUser" + res);
// JSONObject obj = new JSONObject(res);
} catch (UnsupportedEncodingException e1) {
// Couldn't properly decode data to string
e1.printStackTrace();
}
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
};
Log.e(TAG, "uploadUser: near volley new request ");
// Adding request to request queue
Volley.newRequestQueue(this).add(jsonObjReq);
}
将您需要的任何内容与键及其值一起放置在js对象中
Put anything you need in the js object with key and its values
这篇关于Android Volley POST参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!