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":"[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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I have to go with JSONObject only because in Rest URl i have only one set of arguments .. like { "firstName":"parent111", "lastName":"sadfsdf", "email":"[email protected]", "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");
so in that case it will be good to use volley JsonObjectRequest.
can you please suggest me any link how to make JsonObjectRequest like what i did for DefaultHttpClient.
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?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.