Jaccs
Jaccs

Reputation: 1072

How to pass array of variables to REST URL in android?

I have to make registration using REST URL. REST services are written in Java now i have to pass the set of parameters in that secGameIds parameter is like this [100,102]. Example registration using Insomnia:::

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

How should i provide secGameIds parameter value is it a ArrayList or Array?

for remaining values i have created JSONObject class object and adding values to that object and 'm appending that object to 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);
}

where as for secGameId i have tried like below,

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

-- gives me an error in back-end like "nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of int[] out of VALUE_NUMBER_INT token"

I even tried by using

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

and passing to value...

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

again at server side i kicked with the same error.

Can anyone help me?

Upvotes: 2

Views: 1573

Answers (1)

Malik
Malik

Reputation: 5043

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;
}

Hope you can convert the JSONArray to HashMap. If you instead need to post it as a JSONArray itself, then OkHttp library will help you.

Upvotes: 1

Related Questions