【发布时间】:2021-11-07 04:01:37
【问题描述】:
亲爱的 Android 开发者您好! 我现在正在尝试使用改造从我的网络服务中获取数据。但是当我向我的网络服务发送呼叫时,回调函数总是显示 onFailure。
我仔细创建了我的改造类,但在获取数据方面仍然存在问题。另外,我获得了互联网许可。
在这里你可以看到我的改造课程:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
public class ApiUtils {
public static final String BASE_URL = "http://huseyinozkoc.com/";
//bu base link ana link olmalı alt kolları interface içerisinde belirtilir.
public static RetrofitInterface getRetrofitInterface() {
return RetrofitClient.getClient(BASE_URL).create(RetrofitInterface.class);
}
}
public interface RetrofitInterface {
@GET("turkey/get_All_details.php")
Call<PlaceCevap> getAllPlaces();
}
正如我之前提到的,我检查了所有的改装课程,但没有发现任何问题。
然后我仔细尝试检查我的 MainActivity,但仍然没有发现任何异常。
这是我的主要活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.baba);
places = new ArrayList<>();
recyclerView.setLayoutManager(new LinearLayoutManager(this.getApplicationContext()));
recyclerView.setHasFixedSize(true);
placeInterface = ApiUtils.getRetrofitInterface();
placeInterface.getAllPlaces().enqueue(new Callback<PlaceCevap>() {
@Override
public void onResponse(Call<PlaceCevap> call, Response<PlaceCevap> response) {
places = response.body().getPlaces();
adapter= new PlaceAdapter(places);
recyclerView.setAdapter(adapter);
Toast.makeText(MainActivity.this, "got response" , Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<PlaceCevap> call, Throwable t) {
Toast.makeText(MainActivity.this, "fail" , Toast.LENGTH_SHORT).show();
}
});
因此,亲爱的开发人员,为什么我没有从 Web 服务获取数据?它的原因是什么?请写出! 大家好,开发过程顺利!
【问题讨论】:
-
能否在 onFailure 中添加错误日志语句?例如:
Log.e("Unable to load data", t);并在这里分享堆栈跟踪? -
@Kilian 我做了并接受了这个错误:E/Unable to load data: java.net.UnknownServiceException: CLEARTEXT communication to huseyinozkoc.com not allowed by network security policy.
标签: java android kotlin web-services retrofit2