gpt4 book ai didi

java - 我在 Android Studio 中使用 Wea​​therstack API 在 Retrofit 中得到空值

转载 作者:行者123 更新时间:2023-12-01 17:58:55 25 4
gpt4 key购买 nike

 void weatherGET(String key, final String location){
Call<ModelWeather.Current> call = APIService.getWeather(key, location);
call.enqueue(new Callback<ModelWeather.Current>() {
@Override
public void onResponse(Call<ModelWeather.Current> call, Response<ModelWeather.Current> response) {
// Log.d("Entrada en on Response", "Entrada en On Response");
if (response.isSuccessful()) {
currenModelWeather = response.body();
int statusCode = response.code();
Log.d("Status Code", String.valueOf(statusCode));
Log.d("Body", currenModelWeather.toString());
Log.d("temperatura before IF", String.valueOf(currenModelWeather.getTemperature()));
if(currenModelWeather!=null){
temp = (float) currenModelWeather.getTemperature();

Log.d("000000", String.valueOf(currenModelWeather.getTemperature()));
}else{
// Log.d("Es nulo", "Es nulo");
}

}
}
@Override
public void onFailure(Call<ModelWeather.Current> call, Throwable t) {
Log.d("000000", "Fallo en el servidor");
}
});
}

Here is the logcat, so you can see that the request is done well, but I don't understand why then the value comes out 0.The url works correctly, I have tested it both in the browser and in Postman. I have also tried to print other values ​​and I also receive them at 0.

2020-03-13 10:10:33.757 9328-9404/com.example.myoutfit D/OkHttp: --> GET http://api.weatherstack.com/current?access_key=xxx&query=Mountain%20View
2020-03-13 10:10:33.757 9328-9404/com.example.myoutfit D/OkHttp: Accept: application/json
2020-03-13 10:10:33.757 9328-9404/com.example.myoutfit D/OkHttp: --> END GET
2020-03-13 10:10:34.814 9328-9404/com.example.myoutfit D/OkHttp: <-- 200 OK http://api.weatherstack.com/current?access_key=xxx&query=Mountain%20View (1056ms)
2020-03-13 10:10:34.814 9328-9404/com.example.myoutfit D/OkHttp: Date: Fri, 13 Mar 2020 09:10:33 GMT
2020-03-13 10:10:34.814 9328-9404/com.example.myoutfit D/OkHttp: Content-Type: application/json; Charset=UTF-8
2020-03-13 10:10:34.814 9328-9404/com.example.myoutfit D/OkHttp: Transfer-Encoding: chunked
2020-03-13 10:10:34.814 9328-9404/com.example.myoutfit D/OkHttp: Connection: keep-alive
2020-03-13 10:10:34.815 9328-9404/com.example.myoutfit D/OkHttp: Set-Cookie: __cfduid=d5bd7bf9deadd8729888d1bd90dd062241584090633; expires=Sun, 12-Apr-20 09:10:33 GMT; path=/; domain=.weatherstack.com; HttpOnly; SameSite=Lax
2020-03-13 10:10:34.815 9328-9404/com.example.myoutfit D/OkHttp: x-apilayer-transaction-id: bfeee205-c2e8-4cb8-992b-e1ef6ccd0108
2020-03-13 10:10:34.815 9328-9404/com.example.myoutfit D/OkHttp: access-control-allow-methods: GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS
2020-03-13 10:10:34.815 9328-9404/com.example.myoutfit D/OkHttp: access-control-allow-origin: *
2020-03-13 10:10:34.815 9328-9404/com.example.myoutfit D/OkHttp: x-request-time: 0.359
2020-03-13 10:10:34.816 9328-9404/com.example.myoutfit D/OkHttp: CF-Cache-Status: DYNAMIC
2020-03-13 10:10:34.816 9328-9404/com.example.myoutfit D/OkHttp: Server: cloudflare
2020-03-13 10:10:34.816 9328-9404/com.example.myoutfit D/OkHttp: CF-RAY: 57349f596e5eff5c-MAD
2020-03-13 10:10:34.829 9328-9404/com.example.myoutfit D/OkHttp: {"request":{"type":"City","query":"Mountain View, United States of America","language":"en","unit":"m"},"location":{"name":"Mountain View","country":"United States of America","region":"California","lat":"37.386","lon":"-122.083","timezone_id":"America\/Los_Angeles","localtime":"2020-03-13 02:10","localtime_epoch":1584065400,"utc_offset":"-7.0"},"current":{"observation_time":"09:10 AM","temperature":15,"weather_code":113,"weather_icons":["https:\/\/assets.weatherstack.com\/images\/wsymbols01_png_64\/wsymbol_0008_clear_sky_night.png"],"weather_descriptions":["Clear"],"wind_speed":3,"wind_degree":280,"wind_dir":"W","pressure":1008,"precip":0,"humidity":64,"cloudcover":2,"feelslike":15,"uv_index":1,"visibility":10,"is_day":"no"}}
2020-03-13 10:10:34.829 9328-9404/com.example.myoutfit D/OkHttp: <-- END HTTP (736-byte body)
2020-03-13 10:10:35.108 9328-9328/com.example.myoutfit I/Choreographer: Skipped 38 frames! The application may be doing too much work on its main thread.
2020-03-13 10:10:35.296 9328-9328/com.example.myoutfit D/Status Code: 200
2020-03-13 10:10:35.296 9328-9328/com.example.myoutfit D/Body: com.example.myoutfit.ModelWeather$Current@bab8d40
2020-03-13 10:10:35.296 9328-9328/com.example.myoutfit D/temperatura before IF: 0.0
2020-03-13 10:10:35.296 9328-9328/com.example.myoutfit D/000000: 0.0
2020-03-13 10:10:35.400 9328-9400/com.example.myoutfit I/OpenGLRenderer: Davey! duration=752ms; Flags=0, IntendedVsync=78835921494383, Vsync=78836554827691, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=78836567851400, AnimationStart=78836567914500, PerformTraversalsStart=78836567950800, DrawStart=78836568134400, SyncQueued=78836568988800, SyncStart=78836754733200, IssueDrawCommandsStart=78836754830300, SwapBuffers=78836846808900, FrameCompleted=78836859646900, DequeueBufferDuration=1820000, QueueBufferDuration=775000,

最佳答案

我的猜测是您没有正确的响应模型。它应该看起来像这样:

class Response {
@SerializedName("request")
ModelWeather.Current watherModel;
}

您的代码应如下所示:

 void weatherGET(String key, final String location){
Call<Response> call = APIService.getWeather(key, location);
call.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, Response<Response> response) {
// Log.d("Entrada en on Response", "Entrada en On Response");
if (response.isSuccessful()) {
currenModelWeather = response.body.watherModel();
int statusCode = response.code();
Log.d("Status Code", String.valueOf(statusCode));
Log.d("Body", currenModelWeather.toString());
Log.d("temperatura before IF", String.valueOf(currenModelWeather.getTemperature()));
if(currenModelWeather!=null){
temp = (float) currenModelWeather.getTemperature();

Log.d("000000", String.valueOf(currenModelWeather.getTemperature()));
}else{
// Log.d("Es nulo", "Es nulo");
}

}
}
@Override
public void onFailure(Call<Response> call, Throwable t) {
Log.d("000000", "Fallo en el servidor");
}
});
}

关于java - 我在 Android Studio 中使用 Wea​​therstack API 在 Retrofit 中得到空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com