【问题标题】:Parse data from OpenWeatherMap, using retrofit2 and rxJava2从 OpenWeatherMap 解析数据,使用 retrofit2 和 rxJava2
【发布时间】:2017-11-15 08:51:27
【问题描述】:

我已经开始学习 Retrofit 和 RxJava,我决定创建 weatherApp。现在我的目标是从 OpenWeatherMap Api 检索天气数据。这是我的代码: 对于 API:

package com.example.aldres.workingwithapis.Api;

import com.example.aldres.workingwithapis.models.WeatherData;

import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;

public interface Api {
    @GET("weather?")
    Observable<WeatherData> getWeatherData(@Query("q") String city);
}

我的 WeatherData 模型的代码:

package com.example.aldres.workingwithapis.models;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class WeatherData {

    @SerializedName("coord")
    @Expose
    private Coord coord;
    @SerializedName("weather")
    @Expose
    private List<Weather> weather = null;
    @SerializedName("base")
    @Expose
    private String base;
    @SerializedName("main")
    @Expose
    private Main main;
    @SerializedName("visibility")
    @Expose
    private Integer visibility;
    @SerializedName("wind")
    @Expose
    private Wind wind;
    @SerializedName("clouds")
    @Expose
    private Clouds clouds;
    @SerializedName("dt")
    @Expose
    private Integer dt;
    @SerializedName("sys")
    @Expose
    private Sys sys;
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("cod")
    @Expose
    private Integer cod;

    public Coord getCoord() {
        return coord;
    }

    public void setCoord(Coord coord) {
        this.coord = coord;
    }

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

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

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public Main getMain() {
        return main;
    }

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

    public Integer getVisibility() {
        return visibility;
    }

    public void setVisibility(Integer visibility) {
        this.visibility = visibility;
    }

    public Wind getWind() {
        return wind;
    }

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

    public Clouds getClouds() {
        return clouds;
    }

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

    public Integer getDt() {
        return dt;
    }

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

    public Sys getSys() {
        return sys;
    }

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

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getCod() {
        return cod;
    }

    public void setCod(Integer cod) {
        this.cod = cod;
    }

}

还有我的 mainActivity:

package com.example.aldres.workingwithapis;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.example.aldres.workingwithapis.Api.Api;
import com.example.aldres.workingwithapis.models.WeatherData;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private TextView middleText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        middleText = findViewById(R.id.middleText);

        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://api.openweathermap.org/data/2.5/")
                .build();
        Api apiService = retrofit.create(Api.class);

        Observable<WeatherData> observable = apiService.getWeatherData("London");

                observable
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<WeatherData>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(WeatherData weatherData) {
                        middleText.setText(weatherData.getWind());
                    }
                });
    }
}

此时,我的应用程序应该在我的 TextView 中间文本中显示带有风参数的字符串,但是当我启动我的应用程序时,它启动时没有任何错误,但我在 TextView 中没有我的文本。我做错了什么?

【问题讨论】:

  • 介意包括错误吗?
  • 我的 logcat 没有错误,这是个问题。
  • 你应该使用.subscribeOn(Schedulers.io())。你的api需要密钥吗? api.openweathermap.org/data/2.5/London??
  • API 调用应该是:api.openweathermap.org/data/2.5/weather/q={我的城市名称}。我已将.subscribeOn(Schedulers.newThread()) 更改为.subscribeOn(Schedulers.io()),但还是一样。
  • 您是否已将 xml 中的 middleText 默认可见性更改为不可见或消失?

标签: java android retrofit2 rx-java2


【解决方案1】:

您的请求可能在您不知道的情况下出错,因为您忽略了 onError 方法。 在该方法中添加e.printStackTrace(),看看是不是这样。

【讨论】:

  • 哦,我明白了,我收到retrofit2.adapter.rxjava.HttpException: HTTP 401 Unauthorized
  • 我明白了,我需要一个 api 密钥,但我没有注意到。这是我的问题,我想。我应该在我的应用中的哪里添加 apiKey?
  • @Aldres 是的,您需要 api 密钥 api.openweathermap.org/data/2.5/forecast?id=524901&APPID=1111111111 除了切换到 .subscribeOn(Schedulers.io()) 之外,还会导致其阻塞调用
  • 哦,我现在明白了。我找到了我的 api 密钥,更改查询的最佳方法是什么,所以它将包含 api 密钥?
  • @Aldres 喜欢Observable&lt;WeatherData&gt; getWeatherData(@Query("q") String city,@Query("APPID") String yourkey);!
猜你喜欢
  • 2021-08-08
  • 2018-07-06
  • 2017-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
相关资源
最近更新 更多