用于截取webService参数的android pass数组

用于截取webService参数的android pass数组,android,android-volley,Android,Android Volley,我需要发送这个作为我的后参数使用截击 { "userId": "xxx-xxx-xxx", "categories": ["Music"] } 但是在创建了body参数之后,我得到了[on real POST req] { "userId": "xxx-xxx-xxx", "categories": "[Ljava.lang.String;@30f7436c" } 这是我的请求生成器 public class RBGetLikesByCategory exte

我需要发送这个作为我的后参数使用截击

{
    "userId": "xxx-xxx-xxx",
    "categories": ["Music"]
}
但是在创建了body参数之后,我得到了[on real POST req]

{
    "userId": "xxx-xxx-xxx",
    "categories": "[Ljava.lang.String;@30f7436c"
}
这是我的请求生成器

public class RBGetLikesByCategory extends BaseRequestBuilder  {
    public static JSONObject getMyLikesForCategory(String[] categories)
    {
        JSONObject reqData = new JSONObject();
        try {
            reqData.put("userId", getLoginId());
            reqData.put("categories", categories.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return reqData;
    }
}
在这里我称之为请求

RequestQueue rq = Volley.newRequestQueue(getActivity());
    String[] cats = {"Music"};
    JSONObject reqObject = RBGetLikesByCategory.getMyLikesForCategory(cats);

    MyAuthenticatedRequest jsObjRequest = new MyAuthenticatedRequest
            (Request.Method.POST, MyConstants.WebServiceEndPoints.MY_LIKES_URL, reqObject, new Response.Listener<JSONObject>() { ...}
RequestQueue rq=Volley.newRequestQueue(getActivity());
字符串[]猫={“音乐”};
JSONObject reqObject=RBGetLikesByCategory.getMyLikesForCategory(CAT);
MyAuthenticatedRequest jsObjRequest=新的MyAuthenticatedRequest
(Request.Method.POST,MyConstants.WebServiceEndPoints.MY_LIKES_URL,reqObject,new Response.Listener(){…}
很明显,我构造了错误的数组,如何正确解析该数组以满足请求?

似乎categories(“categories”:[“Music”])是一个JSONArray,因此您需要像这样添加它-

public static JSONObject getMyLikesForCategory(String[] categories)
    {
        JSONObject reqData = new JSONObject();
        JSONArray categoryList = new JSONArray();
        try {
            reqData.put("userId", getLoginId());

        //add all items in json array 
        for(int index = 0; index < categories.length; index++){
            categoryList.put(categories[index]);
        }

        reqData.put("categories", categoryList);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return reqData;
    }
公共静态JSONObject getMyLikesForCategory(字符串[]类别)
{
JSONObject reqData=新的JSONObject();
JSONArray categoryList=新的JSONArray();
试一试{
requdata.put(“userId”,getLoginId());
//在json数组中添加所有项
对于(int index=0;index
我希望现在它能起作用。

似乎categories(“categories”:[“Music”])是一个JSONArray,所以您需要像这样添加它-

public static JSONObject getMyLikesForCategory(String[] categories)
    {
        JSONObject reqData = new JSONObject();
        JSONArray categoryList = new JSONArray();
        try {
            reqData.put("userId", getLoginId());

        //add all items in json array 
        for(int index = 0; index < categories.length; index++){
            categoryList.put(categories[index]);
        }

        reqData.put("categories", categoryList);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return reqData;
    }
公共静态JSONObject getMyLikesForCategory(字符串[]类别)
{
JSONObject reqData=新的JSONObject();
JSONArray categoryList=新的JSONArray();
试一试{
requdata.put(“userId”,getLoginId());
//在json数组中添加所有项
对于(int index=0;index
我希望现在它能起作用。

试试看

  public static JSONObject getCategory(String[] categories)
        {
            JSONObject request= new JSONObject();

            try {

       JSONArray jsonArray = new JSONArray(Arrays.asList(categories));  

                request.put("userId", getId());

            request.put("categories", categoryList);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return request;
        }
试试看

  public static JSONObject getCategory(String[] categories)
        {
            JSONObject request= new JSONObject();

            try {

       JSONArray jsonArray = new JSONArray(Arrays.asList(categories));  

                request.put("userId", getId());

            request.put("categories", categoryList);

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return request;
        }