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("url")
void getLoginResponse(@Field("username") String username , @Field("password") String password,
@Field("clientId") String clientId , Callback<LoginResponse> cb);
}
RestClient.java
public class RestClient {
private static ApiInterface REST_CLIENT;
private static String BASE_URL = "base_url";
static {
setupRestClient();
}
private RestClient() {}
public static ApiInterface get() {
return REST_CLIENT;
}
private static void setupRestClient() {
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
REST_CLIENT = restAdapter.create(ApiInterface.class);
}
}
and in activity i call
RestClient.get().getLoginResponse(usernameText, passwordText, clientId, new Callback<LoginResponse>() {
@Override
public void success(LoginResponse loginResponse, Response response) {
Toast.makeText(getApplicationContext(), loginResponse.getToken(), Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
}
});
And in AndroidManifest i set the permission for internet.
I am answering late but it will be useful for others, I preferred to use retrofit 2.
Quit Simple to create instance.
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/
How to make RestClient as a singleton:
Then when you wanna call api you should always call: