为什么我的第一次网络通话失败

时间:2017-12-25 13:23:51

标签: android retrofit

我从我的服务器请求简单的数据列表。我正在使用改造,我已将Stetho设置为网络拦截器来监控网络呼叫。

enter image description here

正如您在Chrome检查窗口中看到的这个屏幕截图所示。我在初始网络呼叫失败后进行的每次网络呼叫都会在第二次自动呼叫服务器并获得成功。这是网络电话的代码。

网络电话功能

public void getLocalAreasFromServer(int districtId) {

        APIService service = ServiceGenerator.createService(APIService.class, this);
        citiesListCall = service.getCitiesList(districtId);
        citiesListCall.enqueue(new Callback<List<CityItem>>() {
            @Override
            public void onResponse(Call<List<CityItem>> call, Response<List<CityItem>> response) {
                if (response.isSuccessful()) {
                    ArrayAdapter<CityItem> adapter = new ArrayAdapter<>(InsertAndUpdateAddressActivity.this,
                            android.R.layout.simple_spinner_dropdown_item, response.body());
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    localAreaSpinner.setAdapter(adapter);

                    if(hasExtra){
                        AddressTable address = getIntent().getParcelableExtra("selected_address");

                        for(CityItem cityObj: response.body()){
                            if(cityObj.getCity_name().equals(address.getLocalArea())){
                                localAreaSpinner.setSelection(adapter.getPosition(cityObj));
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<List<CityItem>> call, Throwable t) {
                t.getStackTrace();
            }
        });
    } 

API服务接口

public interface APIService {

    @GET("get_cities_list")
    Call<List<CityItem>> getCitiesList(@Query("district_id") int district_id);

}

ServiceGenerator班级

public class ServiceGenerator {

    private static final String BASE_URL = "https://www.baseURL.com/mobile/";

    private static OkHttpClient.Builder httpClient =
            new OkHttpClient.Builder()
                    .addNetworkInterceptor(new StethoInterceptor())
                    .connectTimeout(30,TimeUnit.SECONDS)
                    .readTimeout(30,TimeUnit.SECONDS)
                    .addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request original = chain.request();

                    Request request = original.newBuilder()
                            .header("Authorization", "Basic cmdkZXY6cmcxMjM=")
                            .method(original.method(), original.body())
                            .build();

                    return chain.proceed(request);
                }
            });

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                    .client(httpClient.build());

    private static Retrofit retrofit = builder.build();

    public static <S> S createService(Class<S> serviceClass, Context context) {
        Stetho.initializeWithDefaults(context);
        return retrofit.create(serviceClass);
    }
}

我尝试调试,但永远不会来onFailure方法。但我可以在Logcat中看到以下消息,但它不是从onFailure打印的。

java.io.IOException: Address already in use
at android.net.LocalSocketImpl.bindLocal(Native Method)
at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:306)
at android.net.LocalServerSocket.<init>(LocalServerSocket.java:48)
at com.facebook.stetho.server.LocalSocketServer.bindToSocket(LocalSocketServer.java:142)
at com.facebook.stetho.server.LocalSocketServer.listenOnAddress(LocalSocketServer.java:78)
at com.facebook.stetho.server.LocalSocketServer.run(LocalSocketServer.java:74)
at com.facebook.stetho.server.ServerManager$1.run(ServerManager.java:40)

我的应用程序运行正常,用户没有注意到这一点。但我需要知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

该问题已在Github上报告here

解决方案建议将Stetho添加到您的Proguard规则中:

-keep class com.facebook.stetho.**{ *; }