2

*P.s: Changed references for security matters.

I'm trying to get json from a server but it requires form-data in the body of request, like this: enter image description here

It works when tested in postman, but I just can't figure out how to do this using retrofit2.

Beans:

public class Y {

@SerializedName("id")
private int yId;
@SerializedName("name")
private String yName;


//...
}


public class YList {

@SerializedName("ys")
private List<Y> ys;

//...
}

The service interface is like this:

public interface YService {
@POST("y")
Call<YList> getY()

public static final YService serviceY = new Retrofit.Builder()
        .baseUrl("http://example.com.br/api/x/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(YService.class);
}

And REST method:

YService yService = YService.serviceY;




    yService.getY().enqueue(new Callback<YList>() {
        @Override
        public void onResponse(Call<YList> call, Response<YList> response) {

            if (response.isSuccessful()) {
                //...
            } else {

                //...

            }

        }

        @Override
        public void onFailure(Call<YList> call, Throwable t) {

            //...

        }
    });'

Postman JSON result:

{
"auth": {
  "validation": true
},
"ys": [
{
  "id": 1,
  "name": "#"
}
]
}
4
  • where is your interface? Where is the Call<?> that you have? put more information Commented Jan 7, 2018 at 16:20
  • Code added. Hope someone can help me. @KostasDrak Commented Jan 7, 2018 at 16:53
  • did you read about the @Body retrofit annotation? Commented Jan 7, 2018 at 17:06
  • I tried using getY(@Body AuthRequest.Auth info). In which AuthRequest has an "Auth" inner class with email and password attributes and that didn't work. I guess it's because there's no reference to form-data key name? @KostasDrak Commented Jan 7, 2018 at 17:35

2 Answers 2

2

Retrofit2 Body with from-data

@FormUrlEncoded 
@POST("xxx")//endpoint
Call<xxxx> getxxx(@Field("phone") String phone);
Sign up to request clarification or add additional context in comments.

Comments

1

I managed to find a solution, which turned out to be a lot easier than I thought.

Just crate a class called AuthRequest like this:

public class AuthRequest {

    private static final String TAG = "Auth";
    private static final String EMAIL = "email";
    private static final String PASSWORD = "pass";


    //creates a json-format string
    public static String createAuthJsonString(){

        String info = "{\"auth\":{\"email\":\""+EMAIL+"\",\"password\":\""+PASSWORD+"\"}}";


        Log.i(TAG,info);

        return info;
    }

}

Just add @FormUrlEncoded to @Post and put @Field key and value inside method call:

@FormUrlEncoded
@POST("ys")//endpoint
Call<YList> getY(@Field("info") String info);


// Connection url.
public static final YService serviceY = new Retrofit.Builder()
        .baseUrl("http://example.com.br/api/x/")
        .addConverterFactory(GsonConverterFactory.create())
        .build()
        .create(YService.class);

Using getY() method:

    final YService yService = YService.serviceY;
    //...
    yService.getY(AuthRequest.createAuthJsonString()).enqueue(new 
        Callback<YList>() {

        @Override
        public void onResponse(Call<YList> call, Response<YList> response) {

            if (response.isSuccessful()) {
              //...

            } else {

               //...
            }
        }

        @Override
        public void onFailure(Call<YList> call, Throwable t) {

            //...

        }


    });



}

Returned YList from json perfectly.

Comments

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.