2

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":"abc@bbc.com",
"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?

Jaccs
  • 1,052
  • 3
  • 12
  • 33

1 Answers1

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

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.

Maddy
  • 4,525
  • 4
  • 33
  • 53
  • I have to go with JSONObject only because in Rest URl i have only one set of arguments .. like { "firstName":"parent111", "lastName":"sadfsdf", "email":"abc@bbc.com", "date":"2000-06-09", "phoneNum":"8765654454", "gender":"male", **"secGameIds":[0,0],** "roleId":102 } apart from all input i have a problem with secGameIds variable meaning that by using which data type i would provide input. remaining arguments were accepted th imput like json.put("firstName","xxxxxx"); – Jaccs Aug 13 '16 at 06:03
  • so in that case it will be good to use volley JsonObjectRequest. – Maddy Aug 13 '16 at 06:29
  • can you please suggest me any link how to make JsonObjectRequest like what i did for DefaultHttpClient. – Jaccs Aug 13 '16 at 09:52
  • http://stackoverflow.com/questions/24873718/how-do-i-make-a-volley-jsonobject-request-with-a-custom-object-as-a-parameter – Maddy Aug 13 '16 at 10:23
  • I really can't understand fully what is happening when i make JsonObjectRequest but finally i made a request. is it sent to the server automatically or again we need to write HttpPost? – Jaccs Aug 13 '16 at 13:40
  • I already made login to the same rest client it is working fine but struggles only with registration it is not accepting array of integers parameter. help me with an example if possible. – Jaccs Aug 13 '16 at 13:48