android - 改造 onResponse 和 onFailure 方法没有被调用

标签 android retrofit2 darksky

当我调用 DarkSky API 时,对于某些位置,我的 onResponse 和 onFailure 方法不会被触发。我已经测试了美国的很多地方,一切都很好。我测试了伦敦,这很好,但是当我测试印度钦奈时,这两种方法都没有被触发。我还测试了被调用的 URL,当我将其放入网络浏览器时有响应,但它没有触发应用程序中的 onReponse() 方法。

我看到有人说这是因为调用是异步的,但我不知道为什么它在某些城市可以 100% 一致性工作,而在其他城市则不然。

调用类:


import android.content.Context;
import android.content.SharedPreferences;
import android.widget.TextView;

import androidx.preference.PreferenceManager;

import com.jggdevelopment.simpleweather.BuildConfig;
import com.jggdevelopment.simpleweather.fragments.MasterFragment;
import com.jggdevelopment.simpleweather.models.Forecast;

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

public class WeatherAPIUtils {

    private static String baseUrl = "https://api.darksky.net/forecast/" + BuildConfig.darkSkyAPI_KEY + "/";
    private static TextView temperatureView;
    private static TextView highTemp;
    private static TextView lowTemp;
    private static TextView description;
    private static TextView precipitationChance;
    private static SharedPreferences prefs;

    /**
     * uses retrofit to call the DarkSky API using the API key in the baseURL
     * @return WeatherService
     */
    private static WeatherService getWeatherService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(WeatherService.class);
    }

    /**
     * Pulls weather data from the DarkSky API using the provided location.
     * On success, it updates the views in MasterFragment
     * @param lat latitude of location
     * @param lon longitude of location
     * @param fragment MasterFragment
     */
    public static void getCurrentWeatherData(Double lat, Double lon, final MasterFragment fragment) {
        WeatherService service = getWeatherService();
        prefs = fragment.getActivity().getSharedPreferences("com.jggdevelopment.simpleweather", Context.MODE_PRIVATE);

        if (prefs.getBoolean("useCelsius", false)) {
            service.getWeatherSI(lat, lon, "si").enqueue(new Callback<Forecast>() {
                @Override
                public void onResponse(Call<Forecast> call, Response<Forecast> response) {
                    if (response.isSuccessful()) {
                        fragment.updateConditions(response.body());
                    }
                }

                @Override
                public void onFailure(Call<Forecast> call, Throwable t) {

                }
            });
        } else {
            service.getWeatherImperial(lat, lon).enqueue(new Callback<Forecast>() {
                @Override
                public void onResponse(Call<Forecast> call, Response<Forecast> response) {
                    if (response.isSuccessful()) {
                        fragment.updateConditions(response.body());
                    }
                }

                @Override
                public void onFailure(Call<Forecast> call, Throwable t) {

                }
            });
        }
    }
}

POJO:


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Forecast {

    @SerializedName("currently")
    @Expose
    private Currently currently;
    @SerializedName("daily")
    @Expose
    private Daily daily;
    @SerializedName("flags")
    @Expose
    private Flags flags;
    @SerializedName("hourly")
    @Expose
    private Hourly hourly;
    @SerializedName("latitude")
    @Expose
    private Double latitude;
    @SerializedName("longitude")
    @Expose
    private Double longitude;
    @SerializedName("minutely")
    @Expose
    private Minutely minutely;
    @SerializedName("offset")
    @Expose
    private Integer offset;
    @SerializedName("timezone")
    @Expose
    private String timezone;

    public Currently getCurrently() {
        return currently;
    }

    public void setCurrently(Currently currently) {
        this.currently = currently;
    }

    public Daily getDaily() {
        return daily;
    }

    public void setDaily(Daily daily) {
        this.daily = daily;
    }

    public Flags getFlags() {
        return flags;
    }

    public void setFlags(Flags flags) {
        this.flags = flags;
    }

    public Hourly getHourly() {
        return hourly;
    }

    public void setHourly(Hourly hourly) {
        this.hourly = hourly;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Minutely getMinutely() {
        return minutely;
    }

    public void setMinutely(Minutely minutely) {
        this.minutely = minutely;
    }

    public Integer getOffset() {
        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }

}```

最佳答案

像这样失败时输入登录网址

@Override
        public void onFailure(Call<Response> call, Throwable t) {
            t.printStackTrace();


        }

还添加登录拦截器来记录请求和响应

public static OkHttpClient getHttpClient() {

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);



        //TODO : remove logging interceptors as it is to be used for development purpose
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(300, TimeUnit.SECONDS)
                .readTimeout(300,TimeUnit.SECONDS).
                        addInterceptor(logging).
                        build();

        return client;
    }

像这样添加到这里

 private static WeatherService getWeatherService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(getHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(WeatherService.class);
    }

通过这样做至少您将能够识别问题

关于android - 改造 onResponse 和 onFailure 方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56784205/

相关文章:

Android:验证服务器上的设备谷歌帐户名称

java - Android Room 相对于 Cloud Firestore 在持久本地存储方面的优势是什么?

android - Rx 和改造 : Changing ServiceApi request parameter in request loop started by repeatwhen or repeatuntill

android - 仅使用缓存响应。无网络通话

api - 当 precipType 为 "rain"时,DarkSky 时间机器(历史)数据是否应显示 PrecipAccumulation

android - 调试 Android 折叠工具栏 - insets 和 onResume

java - 使用 onCreateView 中的功能动态更改 View

java - Android studio - 带Retrofit2的外汇汇率API,无法运行应用程序

android - 在 Android Studio 中从 JSON 获取值(value)

api - Dark Sky API 替换推荐?