当我调用DarkSky API时,对于某些位置,不会触发我的onResponse和onFailure方法。我已经在美国测试了无数地点,一切都很好。我测试了伦敦,那很好,但是当我测试印度的钦奈时,两种方法都不会触发。我还测试了被调用的URL,将其放入Web浏览器后会出现响应,但不会触发应用程序中的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;
}
}```
答案 0 :(得分:2)
在失败时输入url
@Override
public void onFailure(Call<Response> call, Throwable t) {
t.printStackTrace();
}
还添加loggin拦截器以记录请求和响应
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);
}
这样做至少可以识别问题