Using Retrofit with an API in Java

In accordance with the information from the official site, I added the last degree and began to develop.

First, I created a model with data that interests me:

public class Data{ String parametr1; //geters and setters ommited } 

the second step was to add a service:

 public interface GitHubService { @GET("/repos/{owner}/{repo}") Call<Data> repoInfos(@Path("user") String owner, @Path("repo") String repo); Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").build(); } 

Thirdly, you need to add an implementation:

 @Service public class GitHubServiceImpl implements GitHubService { final GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class); @Override public Call<DetailDto> repoDetails(String owner, String repo) { return gitHubService.repoDetails(owner, repo); } } 

But there is an error:

 java.lang.IllegalArgumentException: Could not locate ResponseBody converter for class model.Data. 

Here is a complete error log trace

+5
source share
1 answer

For maven dependency:

 <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-gson</artifactId> <version><latest-version></version> </dependency> 

For the code, add ConverterFactory:

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

That should do it.

+2
source

Source: https://habr.com/ru/post/1275919/


All Articles