Android Studio 改造调用失败
Android Studio Retrofit Call Failure
我正在尝试通过 Java 在我的应用程序中使用 Retrofit。我正在使用 this worldtime API. You can call this URL in your browser to see the response : http://worldtimeapi.org/api/timezone/Europe/Istanbul(响应来得太晚,请刷新浏览器)
我将此行添加到我的 AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
我将这些添加到 gradle
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
我只想看到几个响应键,所以我创建了一个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;
}
}
我创建了我的界面ApiService.java
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiService {
@GET("Europe/Istanbul")
Call<TimeForIstanbul> getTime();
}
我只是编辑了我的 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());
}
});
}
}
当我 运行 这个应用程序时,我看到
I/System.out: Error is
java.net.UnknownServiceException: CLEARTEXT communication to worldtimeapi.org not permitted by network security policy
在 logcat 上,所以它转到 onFailure
。我错过了什么?这里有什么问题?我的这个例子的资源是 video
您必须将 https 与 api 一起使用。变化:
private String BASE_URL = "http://worldtimeapi.org/api/timezone/";
至
private String BASE_URL = "https://worldtimeapi.org/api/timezone/";
如果您想与 api 进行明文通信,您必须按照 Android documentation
中的步骤进行操作
android:usesCleartextTraffic="true"
在您的清单应用程序中添加这一行
它将允许在没有 SSL 认证的情况下连接 URL
要解决这个问题,你需要做的是将这个添加到我的 manifest.xml
android:usesCleartextTraffic="true"
但您可能需要使用其他方法,您可以查看 。
我正在尝试通过 Java 在我的应用程序中使用 Retrofit。我正在使用 this worldtime API. You can call this URL in your browser to see the response : http://worldtimeapi.org/api/timezone/Europe/Istanbul(响应来得太晚,请刷新浏览器)
我将此行添加到我的 AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
我将这些添加到 gradle
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
我只想看到几个响应键,所以我创建了一个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;
}
}
我创建了我的界面ApiService.java
import retrofit2.Call;
import retrofit2.http.GET;
public interface ApiService {
@GET("Europe/Istanbul")
Call<TimeForIstanbul> getTime();
}
我只是编辑了我的 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());
}
});
}
}
当我 运行 这个应用程序时,我看到
I/System.out: Error is
java.net.UnknownServiceException: CLEARTEXT communication to worldtimeapi.org not permitted by network security policy
在 logcat 上,所以它转到 onFailure
。我错过了什么?这里有什么问题?我的这个例子的资源是 video
您必须将 https 与 api 一起使用。变化:
private String BASE_URL = "http://worldtimeapi.org/api/timezone/";
至
private String BASE_URL = "https://worldtimeapi.org/api/timezone/";
如果您想与 api 进行明文通信,您必须按照 Android documentation
中的步骤进行操作android:usesCleartextTraffic="true"
在您的清单应用程序中添加这一行 它将允许在没有 SSL 认证的情况下连接 URL
要解决这个问题,你需要做的是将这个添加到我的 manifest.xml
android:usesCleartextTraffic="true"
但您可能需要使用其他方法,您可以查看