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

问题描述:

我必须使用REST URL进行注册。 REST服务是用Java编写的,现在我必须通过那个参数集合,secGameIds参数就像这样[100,102]。使用失眠症的示例注册:::如何在android中将变量数组传递给REST URL?

{ 
"firstName":"parent111", 
"lastName":"sadfsdf", 
"email":"[email protected]", 
"date":"2000-06-09", 
"phoneNum":"8765654454", 
"gender":"male", 
**"secGameIds":[0,0],** 
"roleId":102 
} 

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

剩余值我已经建立的JSONObject类对象,并添加值到该对象和“M附加该对象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:无法反序列化INT []的情况下进行VALUE_NUMBER_INT令牌”

我甚至用

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

和传递价值试过......

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

再次在服务器侧踢我用同样的错误。

任何人都可以帮助我吗?

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库将帮助您。

+0

我有JSONObject的去只是因为REST URL我只有一组参数..喜欢 { “名字”: “parent111”, “姓氏”: “sadfsdf”, “电子邮件” :“[email protected]”, “date”:“2000-06-09”, “phoneNum”:“8765654454”, “gender”:“male”, **“secGameIds”:[0, 0],** “roleId”:102 } 除了所有输入,我有一个secGameIds变量的问题,这意味着通过使用哪种数据类型我会提供输入。 (“firstName”,“xxxxxx”); 其余参数被接受, – Jaccs

+0

所以在这种情况下,使用抽象的JsonObjectRequest会很好。 – Maddy

+0

可以请你给我建议任何链接如何使JsonObjectRequest像我为DefaultHttpClient做的。 – Jaccs