【问题标题】:Android retrofit2.BuiltInConverters$RequestBodyConverter ErrorAndroid retrofit2.BuiltInConverters$RequestBodyConverter 错误
【发布时间】:2018-08-07 14:48:44
【问题描述】:

我正在使用 retrofitopenweather API 制作天气应用程序。

这个Link 引导我调用 API,但我总是遇到 retrofit2.BuiltInConverters$RequestBodyConverter 错误。

我试过这个solution,但不起作用。

我的 API 在这里:

http://api.openweathermap.org/data/2.5/weather?q=Ankara&APPID=6a5ea99c096c7ed68e620675d3eb2e2b

这是我的毕业生:

implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

这是 Weather.java:

public class Weather {

public int id;
public String main;
public String description;
public String icon;

public Weather(int id, String main, String description, String icon) {
    this.id = id;
    this.main = main;
    this.description = description;
    this.icon = icon;
}

public int getId() {
    return id;
}

public String getMain() {
    return main;
}

public String getDescription() {
    return description;
}

public String getIcon() {
    return icon;
}

}

这是 ApiClient.java:

public class ApiClient {

public static final String BASE_URL = WeatherService.BASE_URL;
private static Retrofit retrofit = null;


public static Retrofit getClient() {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

}

这是我的界面:

public interface WeatherService {

//API LINK
String BASE_URL = "http://api.openweathermap.org/data/2.5";
String API_KEY = "6a5ea99c096c7ed68e620675d3eb2e2b";

//API SUBCLASS
@GET("/weather?")
//q=CityName &appid =APIKEY
Call<Weather> getWeatherData(@Query("q") String cityName,@Query("appid") String apikey);

}

最后是 MainActiviy.java:

public TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv);

    //asyncTask.execute(API_KEY);


    WeatherService weatherService = ApiClient.getClient().create(WeatherService.class);

  Call<Weather> call = weatherService.getWeatherData("Ankara",weatherService.API_KEY);

  call.enqueue(new Callback<Weather>() {
      @Override
      public void onResponse(Call<Weather> call, Response<Weather> response) {

          Log.d("onResponse","I am here");

          tv.setText(response.body().getDescription());
      }

      @Override
      public void onFailure(Call<Weather> call, Throwable t) {

      }
  });




}

有什么问题,我该如何解决? 衷心感谢大家

【问题讨论】:

    标签: android retrofit retrofit2 openweathermap


    【解决方案1】:

    试试这个:在 baseurl 的末尾添加/

    基本网址:

     String BASE_URL = "http://api.openweathermap.org/data/2.5/";
    

    请求:

    @GET("weather")
    //q=CityName &appid =APIKEY
    Call<String> getWeatherData(@Query("q") String cityName,@Query("APPID") String apikey);
    

    StringJsonNode 回复,然后尝试转换您的pojo。你会发现你的问题在哪里。

    【讨论】:

    • 程序类型已经存在:retrofit2.BuiltInConverters$RequesyBodyConverter
    • 我试过 POJO。我知道我会做什么,我是堆栈。您的回答无效
    • 嘿@sajib。我想说谢谢你的解决方案。我尝试了所有这些,但没有任何改变。我查看了其他改造示例,它们与我相同。实际上错误是 com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex。我尝试了许多解决方案,因为 multiDexenable true。但它不起作用。我的脑子现在瞎了:D
    猜你喜欢
    • 2018-05-22
    • 1970-01-01
    • 2023-04-05
    • 2017-05-09
    • 2016-12-28
    • 1970-01-01
    • 2017-09-05
    • 2021-09-19
    • 2017-01-20
    相关资源
    最近更新 更多