Skip to content

Android Studio Retrofit Call Failure

I’m trying to use Retrofit in my app via Java. I’m using this worldtime API. You can call this URL in your browser to see the response : (response comes too late, just refresh your browser)

I added this line to my AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

I added these to gradle

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

I only want to see few keys of response, so I created a class


public class TimeForIstanbul {
    public Integer dayOfWeek;
    public String utcDatetime;
    public Integer weekNumber;

    public Integer getDayOfWeek() {
        return dayOfWeek;

    public void setDayOfWeek(Integer dayOfWeek) {
        this.dayOfWeek = dayOfWeek;

    public String getUtcDatetime() {
        return utcDatetime;

    public void setUtcDatetime(String utcDatetime) {
        this.utcDatetime = utcDatetime;

    public Integer getWeekNumber() {
        return weekNumber;

    public void setWeekNumber(Integer weekNumber) {
        this.weekNumber = weekNumber;

I created my interface

import retrofit2.Call;
import retrofit2.http.GET;

public interface ApiService {
    Call<TimeForIstanbul> getTime();

And simply I edited my


import android.os.Bundle;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private Retrofit retrofit;
    private ApiService apiService;
    private String BASE_URL = "";
    private Call<TimeForIstanbul> timeForIstanbulCall;
    private TimeForIstanbul timeForIstanbul;

    protected void onCreate(Bundle savedInstanceState) {
    private void setRetrofitSettings(){
        retrofit = new Retrofit.Builder()
        apiService = retrofit.create(ApiService.class);
        timeForIstanbulCall = apiService.getTime();
        timeForIstanbulCall.enqueue(new Callback<TimeForIstanbul>() {
            public void onResponse(Call<TimeForIstanbul> call, Response<TimeForIstanbul> response) {
                    timeForIstanbul = response.body();

            public void onFailure(Call<TimeForIstanbul> call, Throwable t) {
                System.out.println("Error is ");

And when I run this app, I see

I/System.out: Error is CLEARTEXT communication to not permitted by network security policy

on logcat so it goes to onFailure. What am I missing? What is wrong here? My resource for this example is this video



What you have to do to solve the problem is add this on my manifest.xml


But you may have to use other approaches you can take a look on android-8-cleartext-http-traffic-not-permitted.

8 People found this is helpful