Retrofit Android Not Working And Has No Error
I just implemented the retrofit android library for rest api call but it is not working and has no error. My code is ApiInterface.java public interface ApiInterface { @POST('
Solution 1:
How to make RestClient as a singleton:
publicclassRestClient {
privatestatic ApiInterface REST_CLIENT;
privatestatic String BASE_URL = "base_url";
publicRestClient() {}
publicstatic ApiInterface getInstance() {
//if REST_CLIENT is null then set-up again. if (REST_CLIENT == null) {
setupRestClient();
}
return REST_CLIENT;
}
privatestaticvoidsetupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
REST_CLIENT = restAdapter.create(ApiInterface.class);
}
}
Then when you wanna call api you should always call:
ApiInterfaceapi= RestClient.getInstance();
api.callWhatApiYouWant
Solution 2:
I am answering late but it will be useful for others, I preferred to use retrofit 2.
// Retrofit
compile'com.squareup.retrofit2:retrofit:2.1.0'
// JSON Parsing
compile'com.google.code.gson:gson:2.7'compile'com.squareup.retrofit2:converter-gson:2.1.0'
Quit Simple to create instance.
publicstaticRetrofitgetClient(String baseUrl) {
if (retrofit==null) {
retrofit = newRetrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
Here is Detailed explanation about retrofit 2 android best example and quit simple to understand. http://al-burraq.com/retrofit-android-get-and-post-api-request-tutorial/
Post a Comment for "Retrofit Android Not Working And Has No Error"