试图在OpenWeather API中使用GSON

问题描述 投票:0回答:1

我正在尝试将GSON与OpenWeatherMap API结合使用。我能够将JSON解析为以下代码:

public class List {

@SerializedName("dt")
@Expose
private Integer dt;
@SerializedName("main")
@Expose
private Main main;
@SerializedName("weather")
@Expose
private java.util.List<Weather> weather = null;
@SerializedName("clouds")
@Expose
private Clouds clouds;
@SerializedName("wind")
@Expose
private Wind wind;
@SerializedName("rain")
@Expose
private Rain rain;
@SerializedName("sys")
@Expose
private Sys sys;
@SerializedName("dt_txt")
@Expose
private String dtTxt;

public Integer getDt() {
    return dt;
}

public void setDt(Integer dt) {
    this.dt = dt;
}

public Main getMain() {
    return main;
}

public void setMain(Main main) {
    this.main = main;
}

public java.util.List<Weather> getWeather() {
    return weather;
}

public void setWeather(java.util.List<Weather> weather) {
    this.weather = weather;
}

public Clouds getClouds() {
    return clouds;
}

public void setClouds(Clouds clouds) {
    this.clouds = clouds;
}

public Wind getWind() {
    return wind;
}

public void setWind(Wind wind) {
    this.wind = wind;
}

public Rain getRain() {
    return rain;
}

public void setRain(Rain rain) {
    this.rain = rain;
}

public Sys getSys() {
    return sys;
}

public void setSys(Sys sys) {
    this.sys = sys;
}

public String getDtTxt() {
    return dtTxt;
}

public void setDtTxt(String dtTxt) {
    this.dtTxt = dtTxt;
}

}

公共类Main {

@SerializedName("temp")
@Expose
private Double temp;
@SerializedName("temp_min")
@Expose
private Double tempMin;
@SerializedName("temp_max")
@Expose
private Double tempMax;
@SerializedName("pressure")
@Expose
private Double pressure;
@SerializedName("sea_level")
@Expose
private Double seaLevel;
@SerializedName("grnd_level")
@Expose
private Double grndLevel;
@SerializedName("humidity")
@Expose
private Integer humidity;
@SerializedName("temp_kf")
@Expose
private Integer tempKf;

public Double getTemp() {
    return temp;
}

public void setTemp(Double temp) {
    this.temp = temp;
}

public Double getTempMin() {
    return tempMin;
}

public void setTempMin(Double tempMin) {
    this.tempMin = tempMin;
}

public Double getTempMax() {
    return tempMax;
}

public void setTempMax(Double tempMax) {
    this.tempMax = tempMax;
}

public Double getPressure() {
    return pressure;
}

public void setPressure(Double pressure) {
    this.pressure = pressure;
}

public Double getSeaLevel() {
    return seaLevel;
}

public void setSeaLevel(Double seaLevel) {
    this.seaLevel = seaLevel;
}

public Double getGrndLevel() {
    return grndLevel;
}

public void setGrndLevel(Double grndLevel) {
    this.grndLevel = grndLevel;
}

public Integer getHumidity() {
    return humidity;
}

public void setHumidity(Integer humidity) {
    this.humidity = humidity;
}

public Integer getTempKf() {
    return tempKf;
}

public void setTempKf(Integer tempKf) {
    this.tempKf = tempKf;
}

}

公共课天气{

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("main")
@Expose
private String main;
@SerializedName("description")
@Expose
private String description;
@SerializedName("icon")
@Expose
private String icon;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getMain() {
    return main;
}

public void setMain(String main) {
    this.main = main;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getIcon() {
    return icon;
}

public void setIcon(String icon) {
    this.icon = icon;
}

}

我一直在网上和这里进行搜索,但是我无法找到如何在我的代码中精确地使用GSON:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_weather_app, container,     false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));

    requestJsonObject();

    return view;


}


private void requestJsonObject (){

}

如何将GSON解析准确地添加到我的代码中?我只需要温度,最高,最低,天气和湿度?

编辑:

这里有更多信息。我计划使用带有apikey的常规http连接来获取我的信息。我还计划使用RecyclerView,这是该代码:

public class RecyclerViewAdapter  extends         
RecyclerView.Adapter<RecyclerViewAdapter.CurrentRecycler> {

List<Weather> mCurrentWeatherDataList;

public static class CurrentRecycler extends RecyclerView.ViewHolder{

public TextView location;
public TextView currentTemp;
public TextView currentHumidity;
public TextView currentDescription;
public ImageView currentIcon;

public CurrentRecycler (View view) {
    super (view);

    location =  (TextView) view.findViewById(R.id.current_city_location);
    currentTemp = (TextView) view.findViewById(R.id.current_temperature);
    currentHumidity = (TextView) view.findViewById(R.id.current_humidity);
    currentDescription = (TextView) view.findViewById(R.id.current_weather_description);
    currentIcon = (ImageView) view.findViewById(R.id.current_weather_icon);

}

}

public RecyclerViewAdapter (List<Weather> mCurrentWeatherDataList) {
    this.mCurrentWeatherDataList = mCurrentWeatherDataList;
}

@Override
public CurrentRecycler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);

        final CurrentRecycler  currentRecycler = new CurrentRecycler(view);

        return currentRecycler;
}

@Override
public void onBindViewHolder( CurrentRecycler holder, int position) {

    final Weather currentRecycler = mCurrentWeatherDataList.get(position);
    holder.location.setText(currentRecycler.getDefaultLocation());
    holder.currentTemp.setText((currentRecycler.getDefaultCurrentTemp()));
    holder.currentHumidity.setText(currentRecycler.getDefaultHumidity());
    holder.currentDescription.setText(currentRecycler.getDefaultDescription());


}

@Override
public int getItemCount() {
    return mCurrentWeatherDataList.size();
}

}

android gson openweathermap
1个回答
0
投票

在您的requestJsonObject方法中,使用凌空或okhttp从API中获取天气数据。一旦获得数据为String,就可以使用以下代码:

List list = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().fromJson(str, List.class);
Main main = list.getDt();
main.getTemp();
main.getTempMin();
main.getTempMax();

str表示您的api响应数据。 List表示您的第一个实体类。

此外,我强烈建议您重命名您的类名,切勿尝试使用Java的保留名,使它们有意义。

© www.soinside.com 2019 - 2024. All rights reserved.