gpt4 book ai didi

java - 当我通过改造传递对象时收到空响应

转载 作者:行者123 更新时间:2023-12-02 04:27:34 26 4
gpt4 key购买 nike

当我通过 Retrofit2 传递对象时收到空响应。我收到带有 URL 的请求,但看到内容为空。我不知道为什么...

public interface WeatherAPI {

@GET("current.json")
@Headers("Accept:application/json")
Call<WeatherInfoResponse> getWeather(@Query("key") String key,@Query("q") String location);
}

retrofit = new Retrofit.Builder()
.baseUrl("https://api.apixu.com/v1/")
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
weatherAPI =retrofit.create(WeatherAPI.class);
weatherGET(KEY, locationCurrent);


}


void weatherGET(String key, final String location){
Call<WeatherInfoResponse> call = weatherAPI.getWeather(key,location);
call.enqueue(new Callback<WeatherInfoResponse>() {
@Override
public void onResponse(Call <WeatherInfoResponse> call, Response<WeatherInfoResponse> response) {

weatherInfoResponse = response.body();

Log.d("Temp",String.valueOf(weatherInfoResponse));


Log.d("%%%%%","Entrada");
float temp = weatherInfoResponse.getCurrent().getTemp_c();
}
@Override
public void onFailure(Call<WeatherInfoResponse> call, Throwable t) {
Log.d("%%%%%","Salida");

}
});
}

这是我的错误的 LogCat

ava.lang.NullPointerException: Attempt to invoke virtual method 'float com.example.weatherapi.Current.getTemp_c()' on a null object reference
at com.example.weatherapi.MainActivity$1.onResponse(MainActivity.java:65)

更新

我的POJO类(class),希望对你有帮助

public class WeatherInfoResponse { 
public Location LocationObjectWeather;
public Current CurrentObjectWeather; // Getter Methods public
Location getLocation() { return LocationObjectWeather; } public
Current getCurrent() { return CurrentObjectWeather; } // Setter
// Methods
public void setLocation(Location LocationObjectWeather) {
this.LocationObjectWeather = LocationObjectWeather;
}
public void setCurrent(Current CurrentObjectWeather) {
this.CurrentObjectWeather = CurrentObjectWeather;
}
}

这是示例响应

    {
"location": {
"name": "Glasgow",
"region": "Glasgow City",
"country": "United Kingdom",
"lat": 55.86,
"lon": -4.25
}
}

Any help would be appreciated, Thank you in advance

最佳答案

我建议您使用此在线服务来生成POJO。有关信息,您的对象名称 getter 和 setter 应与 JSON 中相应元素的名称相同。我假设您正在使用 Gson 因为您正在使用 GsonConverterFactory 来解析响应。由于 JSON 响应不完整,但我在这里发布了它应该是什么样的。在您的情况下,这不一样并且缺少一些元素。

示例响应:

   {
"location": {
"name": "Glasgow",
"region": "Glasgow City",
"country": "United Kingdom",
"lat": 55.86,
"lon": -4.25
}
}

上述响应的示例 POJO 应为:

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

public class Location {

@SerializedName("name")
@Expose
private String name;
@SerializedName("region")
@Expose
private String region;
@SerializedName("country")
@Expose
private String country;
@SerializedName("lat")
@Expose
private double lat;
@SerializedName("lon")
@Expose
private double lon;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getRegion() {
return region;
}

public void setRegion(String region) {
this.region = region;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLon() {
return lon;
}

public void setLon(double lon) {
this.lon = lon;
}

}

--

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

public class Example {

@SerializedName("location")
@Expose
private Location location;

public Location getLocation() {
return location;
}

public void setLocation(Location location) {
this.location = location;
}

}

--

这是一篇关于此的好读物。希望这可以帮助你。

http://www.jsonschema2pojo.org/

https://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit

关于java - 当我通过改造传递对象时收到空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56601791/

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