Dia*_*ada 11
只需将响应类型设置为字符串
@GET("api/offers")
Call<String> loadOffers();
Run Code Online (Sandbox Code Playgroud)
代替
@GET("api/offers")
Call<List<Offer>> loadOffers();
Run Code Online (Sandbox Code Playgroud)
将此添加到您的 build.gradle 文件中
implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'
Run Code Online (Sandbox Code Playgroud)
并在 public static Retrofit getRetrofitInstance() {
在函数的末尾添加这些行。重要的部分是转换工厂
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
Run Code Online (Sandbox Code Playgroud)
然后你可以获取你的数据作为 JSON 字符串
MyApi service = RetrofitClientInstance.getRetrofitInstance().create(MyApi.class);
Call<String> callTypes = service.loadOffers();
callTypes.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response {
String urJson = response.body() ;
// DO UR STUFF HERE
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
Run Code Online (Sandbox Code Playgroud)
我希望这能帮到您。
| 归档时间: |
|
| 查看次数: |
1985 次 |
| 最近记录: |