如何在android中将变量数组传递给REST URL?

时间:2016-08-13 04:00:51

标签: java android arrays json rest

我必须使用REST URL进行注册。 REST服务是用Java编写的,现在我必须传递一组参数,因为secGameIds参数就像这样[100,102]。使用Insomnia :::

注册的示例
{
"firstName":"parent111",
"lastName":"sadfsdf",
"email":"abc@bbc.com",
"date":"2000-06-09",
"phoneNum":"8765654454",
"gender":"male",
**"secGameIds":[0,0],**
"roleId":102
}

我应该如何提供secGameIds参数值是一个ArrayList还是Array?

对于剩余的值我创建了JSONObject类对象并向该对象添加值并将该对象附加到url

{
    JSONObject json = new JSONObject();
    json.put("fistName","aaa");
    ..
    ..
    HttpPost post = new HttpPost(uri);
    post.setHeader("Content-type", "application/json");
    post.setEntity(new StringEntity(json.toString(), "UTF-8"));
    DefaultHttpClient client = new DefaultHttpClient();
    httpresponse = client.execute(post);
}

至于secGameId,我试过如下,

{
int[] secGameId = {100,102};
}

- 在后端给我一个错误,比如“嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无法从VALUE_NUMBER_INT标记中反序列化int []的实例”

我甚至尝试使用

{
ArrayList<Integer> secGameId = new ArrayList<String>();
secGameId.add(100);
secGameId.add(102);
}

并传递给价值......

{
json.put("secGameIds":secGameId)
}

再次在服务器端我踢了同样的错误。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

public static String httpPost(HashMap<String, String> map, String url,String token) {
    Log.e("call ", "running");
    HttpRequest request;
    if(token!=null){
        request = HttpRequest.post(url).accept("application/json")
            .header("Authorization", "Token " + AppInfo.token).form(map);
    }
    else 
        request = HttpRequest.post(url).accept("application/json").form(map);

    int responseCode = request.code();
    String text = request.body();

    Log.e("response", " "+responseCode+ " "+ text);

    if(responseCode==400){
        return "invalid_tocken";
    }
    else if(responseCode<200 || responseCode>=300) {
        return "error";
    }
    return text;
}

希望您可以将JSONArray转换为HashMap。如果您需要将其作为JSONArray本身发布,那么OkHttp库将为您提供帮助。