0

I'm using Retrofit2.0 for making GET request to my REST URL. I don't need to pass any params to url for making the request. How could on can make this type of request?

Here is my code what i 've done!

Interface ::

public interface AllRolesAPI {
    @GET("/SportsApp/allroles")
    Call<AllRolesParams> getAllRoles();
}

Class ::: I created a class using Pojo library it contains all the variables with setter and getter methods.

public void requestRoles() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ENDPOINT)
                .build();

        AllRolesAPI allRolesParams = retrofit.create(AllRolesAPI.class);
        Call<AllRolesParams> allRolesParamsCall = allRolesParams.getAllRoles();
        allRolesParamsCall.enqueue(new Callback<AllRolesParams>() {
            @Override
            public void onResponse(Call<AllRolesParams> call, Response<AllRolesParams> response) {
                //response.body().getErrDesc();
                Log.v("SignupActivity", "Response :: " + response.body().getErrDesc());
            }

            @Override
            public void onFailure(Call<AllRolesParams> call, Throwable t) {
                Log.v("SignupActivity", "Failure :: ");
            }
        });
    }

When I create a request like above I have got this error in console ::

java.lang.IllegalArgumentException: Unable to create converter for class com.acknotech.kiran.navigationdrawer.AllRolesParams.

2 Answers 2

1

If your API's responses are JSON, you need to add

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(ENDPOINT)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

In order to be able to use GsonConverterFactory, you need to add a gradle dependency. Check this. In your case is

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

(2.1.0 is the latest version at the time of this writing)

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

8 Comments

When i change the code like what you said... enqueue shows the error, like cannot find the symbol.
yes, because you need to add that dependency to the build.gradle file, and sync the project
No.. still getting error. Retrofit retrofit = new Retrofit.Builder() .baseUrl(ENDPOINT) .addConverterFactory(GsonConverterFactory.create()) .build(); AllRolesAPI allRolesAPI = retrofit.create(AllRolesAPI.class); Call<AllRolesParams> allrolesResponseCall = allRolesAPI.getAllRoles(); This is what the code now how should i make the get request without any parameter passing.
Add the complete stack trace
AllRolesAPI public interface AllRolesAPI { @GET("/SportsApp/allroles") Call<AllRolesParams> getAllRoles(); } AllRolesParams.java Have setter and getters methods of response params. Retrofit retrofit = new Retrofit.Builder() .baseUrl(ENDPOINT) .addConverterFactory(GsonConverterFactory.create()) .build(); AllRolesAPI allRolesAPI = retrofit.create(AllRolesAPI.class); Call<AllRolesParams> allrolesResponseCall = allRolesAPI.getAllRoles(); This is the full stack trace.
|
0

Quoting official docs:

By default, Retrofit can only deserialize HTTP bodies into OkHttp's ResponseBody type and it can only accept its RequestBody type for @Body. Converters can be added to support other types. Six sibling modules adapt popular serialization libraries for your convenience.

Gson: com.squareup.retrofit2:converter-gson
Jackson:com.squareup.retrofit2:converter-jackson
Moshi:com.squareup.retrofit2:converter-moshi
Protobuf:com.squareup.retrofit2:converter-protobuf
Wire:com.squareup.retrofit2:converter-wire
Simple XML:com.squareup.retrofit2:converter-simplexml Scalars (primitives, boxed,and String): com.squareup.retrofit2:converter-scalars

You are trying to parse JSON without any converter. There are various converts you can use with Retrofit. Most Popular is Gson Converter from Google. To make your code work create Retrofit adapter like this:

adapter = new Retrofit.Builder() //in your case replace adapter with Retrofit retrofit
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

Also make sure to include these dependencies:

compile 'com.google.code.gson:gson:2.6.2'      
compile 'com.squareup.retrofit2:retrofit:2.1.0'       
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

Hope it works.You can refer to official retrofit docs,this guide and gson guide for more information.

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.