无法使用改造库获取数据
Posted
技术标签:
【中文标题】无法使用改造库获取数据【英文标题】:Unable to get data using retrofit library 【发布时间】:2018-04-20 16:23:44 【问题描述】:Hai Iam 是改造新手,旨在尝试使用改造库从 url 获取数据,这是我的 json 数据
我想在每个数组中显示名称。
这里是 gson 转换的 Pojo 类
RestResponse.java
public class RestResponse
@SerializedName("messages")
@Expose
private List<String> messages = null;
@SerializedName("result")
@Expose
private List<Result> result = null;
public List<String> getMessages()
return messages;
public void setMessages(List<String> messages)
this.messages = messages;
public List<Result> getResult()
return result;
public void setResult(List<Result> result)
this.result = result;
和第二个Result.java
public class Result
@SerializedName("name")
@Expose
private String name;
@SerializedName("alpha2_code")
@Expose
private String alpha2Code;
@SerializedName("alpha3_code")
@Expose
private String alpha3Code;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getAlpha2Code()
return alpha2Code;
public void setAlpha2Code(String alpha2Code)
this.alpha2Code = alpha2Code;
public String getAlpha3Code()
return alpha3Code;
public void setAlpha3Code(String alpha3Code)
this.alpha3Code = alpha3Code;
最后是 RestResponseMain.java // Gson Converter 生成的 Example.java 文件
public class RestResponseMain
@SerializedName("RestResponse")
@Expose
private RestResponse restResponse;
public RestResponse getRestResponse()
return restResponse;
public void setRestResponse(RestResponse restResponse)
this.restResponse = restResponse;
通过使用上述类,我试图检索数据
实际网址是:http://services.groupkt.com/country/get/all
我的 Apiclenit 文件是:
public class ApiClient
public static final String BaseUrl="http://services.groupkt.com";
public static Retrofit retrofit = null;
public static Retrofit getData()
if(retrofit==null)
retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build();
return retrofit;
ApiInterface 是
public interface ApiInterface
@GET("/country/get/all")
Call<RestResponse> getData();
在 MainActivity 中我使用的操作是
ApiInterface apiInterface = ApiClient.getData().create(ApiInterface.class);
Call<RestResponse> call = apiInterface.getData();
pDialog.show();
call.enqueue(new Callback<RestResponse>()
@Override
public void onResponse(Call<RestResponse> call, Response<RestResponse> response)
pDialog.dismiss();
List<Result> list = response.body().getResult();
MyRecyclerRetrofitAdapter adapter = new MyRecyclerRetrofitAdapter(MainActivity.this,list);
rView.setAdapter(adapter);
@Override
public void onFailure(Call<RestResponse> call, Throwable t)
);
在我使用的适配器文件中
holder.tvName.setText(data.get(position).getName());//using recycler & card view
最后我没有从上述过程中得到任何回应,我对改造概念很陌生,我正在通过一些在线教程尝试这个。
根据上面代码中的 pojo 类向我建议可能的更改。
【问题讨论】:
【参考方案1】:/** * API调用 */
private static ServiceInterface apiService;
public ServiceInterface callWSAds()
if (!isInternetAvailable())
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
builder.readTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(new Interceptor()
@Override
public okhttp3.Response intercept(Chain chain) throws IOException
Request originalRequest = chain.request();
Request.Builder requestBuilder = originalRequest.newBuilder().method(originalRequest.method(), originalRequest.body());
Request request = requestBuilder.build();
return chain.proceed(request);
);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
OkHttpClient client = builder.build();
Retrofit retrofit;
retrofit = new Retrofit.Builder()
.baseUrl(DEV_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
apiService = retrofit.create(ServiceInterface.class);
return apiService;
long HTTP_TIMEOUT = 80;
callWSAds().getAdsSettingsNew(map).enqueue(new Callback<AdsSetting>() //Need to change here
@Override
public void onResponse(Call<AdsSetting> call, Response<AdsSetting> response)
if (response.body() != null)
saveResponseOffline(response.body());
setting = getResponseOffline();
launchApp();//To track the launches of application
@Override
public void onFailure(Call<AdsSetting> call, Throwable t)
if (setting == null)
loadJSONFromAsset();
);
此示例包含 POST 方法调用。请使用这种配置,让 API 调用更简单。
【讨论】:
【参考方案2】:你绑定了一个错误的模型类,所以你只需要改变你的基本 url 需要以 "/"
结尾
休息响应
到
RestResponseMain
API 客户端
public class ApiClient
public final String baseUrl="http://services.groupkt.com/";
public static Retrofit retrofit;
public static Retrofit getData()
if (retrofit == null)
retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
在 API 接口和 MainActivty.show 下面
API接口
public interface ApiInterface
@GET("country/get/all")
Call<RestResponseMain> getData();
主活动
ApiInterface apiInterface = ApiClient.getData().create(ApiInterface.class);
Call<RestResponseMain> call = apiInterface.getData();
pDialog.show();
call.enqueue(new Callback<RestResponseMain>()
@Override
public void onResponse(Call<RestResponseMain> call, Response<RestResponseMain> response)
pDialog.dismiss();
List<Result> list = response.body().getResult();
MyRecyclerRetrofitAdapter adapter = new MyRecyclerRetrofitAdapter(MainActivity.this,list);
rView.setAdapter(adapter);
@Override
public void onFailure(Call<RestResponseMain> call, Throwable t)
);
【讨论】:
感谢兄弟 List您的基本网址需要按照改造文档的规定以“/”结尾。
API客户端:
public class ApiClient
public final String baseUrl="http://services.groupkt.com/";
public static Retrofit retrofit;
public static Retrofit getData()
if (retrofit == null)
retrofit = new Retrofit.Builder()
.baseUrl(BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
API接口:
public interface ApiInterface
@GET("country/get/all")
Call<RestResponse> getData();
【讨论】:
以上是关于无法使用改造库获取数据的主要内容,如果未能解决你的问题,请参考以下文章
无法在 android 上使用改造显示 MySQL JSON 数据