2

I have JsonElement jsonElement object which I receive as response from my another GET request. jsonElement.toString(); looks like JSON array:

[{"Id":493,"Number":"380931112233","FriendNumber":"3806822233344"},{"Id":494,"Number":"380931112233","FriendNumber":"3806388886667"}]

I need to send this string via another POST request using Retrofit. How can I send jsonElement or String object via POST request? How should look declaration of my method? For example:

 @POST("/api/geo/getLoc")
    public void getFriendsLocation(/* something */,  Callback<JsonElement> response);
3
  • are you using spring mvc? there is annotation @RequestBody to do that in spring mvc Commented Sep 25, 2015 at 17:04
  • @neustart47: were you able to find solution to this? I am facing same problem. Not sure how I should pass json array. Any pointers would help! Thanks! Commented Jan 29, 2016 at 7:52
  • check my answer to similar question as yours..stackoverflow.com/questions/32775756/… Commented Mar 29, 2016 at 19:12

1 Answer 1

2

If you are sending data over request body your implementation should be like this:

  1. Define model according to fields (CaseSensitive "Name" -> String Name etc )
  2. set your api function also like that

    @POST("/api/geo/getLoc")
    public void getFriendsLocation(@Body YourClass classObject,   Callback<JsonElement> response);
    
  3. Use directly your created class object on post request

    getFriendsLocation(yourClassObjectThatIncludesFields, new Callback .... )
    

If your sending data over params You can do this with Gson.

  1. Lets say you have a class that have fields like id , number and FriendNumber.Define a function :

    public static Map<String, Object> getMapFromObject(Object o) {
        Gson gson = new Gson();
        Type stringObjectMap = new TypeToken<Map<String, Object>>() {
         }.getType();
        return gson.fromJson(gson.toJson(o), stringObjectMap);
    }
    
  2. set your api function also like that

    @POST("/api/geo/getLoc")
    public void getFriendsLocation(@QueryMap Map<String, Object>,     Callback<JsonElement> response);
    
  3. When you are sending post request create object from your fields call this function like below here

    getFriendsLocation(getMapFromObject(yourClassObjectThatIncludesFields), new Callback .... )
    

I didnt write whole code which includes class definition and Callback function because they are up to your customization. I assume that you need to send over body so try the first way.

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

4 Comments

Thak you for your answer. I created a model class, after create an object of model class and handed it to getMapFromObject function. As result of work of this function i receive correct Map<String, Object> object. It looked like: [0]:"FriendNumber" -> "380935275259" [1]: "Number" -> "380936831127" [2]: "Id" -> "516.0". I saw that in debug mode. But as result of request i recieve: 500 Internal Server Error. Server work for sure right, so the problem in my code. What's can be wrong ?
Are you doing that string parsing by hand on serverside ? Are you expecting that fields at the body of request or as parameters ? If you can share example working post request using software like Postman or etc. I can help more.
I don't know how server side working,all time i communicate with it with JSON arrays. This this the example from Postman: snag.gy/JYwTu.jpg
I have edited answer according to that given extra information. You should implement the first way.

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.