Retrofit - Clarification on how to write with Retrofit

81 Views Asked by At

API Interface

public interface YourUsersApi {
   @GET("/name/{name}")
   public void getName(@Path("name") String user, Callback<pojo> response);

    @GET("/gender/{gender}")
   public void getName(@Path("gender") String user, Callback<pojo> response);
}

MainActivity

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://yourserveraddress.com").build();
YourUsersApi yourUsersApi = restAdapter.create(YourUsersApi.class);

If I want to acces "https://yourserveraddress.com/users/matthew", I would add this code into MainActivity:

            git.getPlaces(KEY, new Callback<pojo>() {
                @Override
                public void success(pojo pojo, Response response) {
                    Log.d("test1", "success");
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.d("test1", error.toString());
                }
            });

Two problems arise though:

  1. How would I do it if I want to acces "https://yourserveraddress.com/users/matthew/gender/male"? do I need to concatenate the interface methods together or some sort?

  2. Is there a much efficient way to write the interface methods? I feel like creating an anonymous class for each one is inefficient.

1

There are 1 best solutions below

2
On

There are multiple ways we can use retrofit. If you do not want to use anonymous class so you can also follow below code.

API Interface

@GET("/name/{name}")
public Pojo GetName (@Path ("name") String user);

final Pojo pojo = git.getPlaces(KEY);

For this you can get more idea about it from this link : https://medium.com/@shelajev/how-to-make-http-calls-on-android-with-retrofit-2-cfc4a67c6254#.llvydsa8a