Hai Iam刚刚改造并试图使用改造库从网址获取数据,这里是我的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类建议可能的更改。
答案 0 :(得分: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调用更简单。
答案 1 :(得分:1)
你绑定了一个错误的模型calss,所以你只需要改变你的基本网址需要以"/"
结束
RestResponse
到
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();
}
MainActivity
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) {
}
});
答案 2 :(得分:1)
您的基本网址需要以Retrofit文档中指定的“/”结尾。
APIClient:
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;
}
}
APIInterface:
public interface ApiInterface {
@GET("country/get/all")
Call<RestResponse> getData();
}