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 : http://worldtimeapi.org/api/timezone/Europe/Istanbul (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 TimeForIstanbul.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class TimeForIstanbul {
@SerializedName("day_of_week")
@Expose
public Integer dayOfWeek;
@SerializedName("utc_datetime")
@Expose
public String utcDatetime;
@SerializedName("week_number")
@Expose
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 ApiService.java
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiService {
@GET("Europe/Istanbul")
Call<TimeForIstanbul> getTime();
}
And simply I edited my MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
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 = "http://worldtimeapi.org/api/timezone/";
private Call<TimeForIstanbul> timeForIstanbulCall;
private TimeForIstanbul timeForIstanbul;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRetrofitSettings();
}
private void setRetrofitSettings(){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService = retrofit.create(ApiService.class);
timeForIstanbulCall = apiService.getTime();
timeForIstanbulCall.enqueue(new Callback<TimeForIstanbul>() {
@Override
public void onResponse(Call<TimeForIstanbul> call, Response<TimeForIstanbul> response) {
if(response.isSuccessful()){
timeForIstanbul = response.body();
System.out.println(timeForIstanbul.getDayOfWeek());
}
}
@Override
public void onFailure(Call<TimeForIstanbul> call, Throwable t) {
System.out.println("Error is ");
System.out.println(t.toString());
}
});
}
}
And when I run this app, I see
I/System.out: Error is
java.net.UnknownServiceException: CLEARTEXT communication to worldtimeapi.org 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
CodePudding user response:
android:usesCleartextTraffic="true"
Add This line in your manifest application It will allow to connect with without SSL certified URL
CodePudding user response:
You have to use https with that api. Change:
private String BASE_URL = "http://worldtimeapi.org/api/timezone/";
to
private String BASE_URL = "https://worldtimeapi.org/api/timezone/";
If you want to have a cleartext communication with the api you have to follow the steps from the Android documentation
CodePudding user response:
What you have to do to solve the problem is add this on my manifest.xml
android:usesCleartextTraffic="true"
But you may have to use other approaches you can take a look on android-8-cleartext-http-traffic-not-permitted.