Android 无法使用改装库获取数据
Hai是一个新的改型者,目的是尝试使用改型库从url获取数据,这是我的json数据 我想在每个数组中显示名称 下面是gson转换的Pojo类 resresponse.javaAndroid 无法使用改装库获取数据,android,gson,retrofit,pojo,Android,Gson,Retrofit,Pojo,Hai是一个新的改型者,目的是尝试使用改型库从url获取数据,这是我的json数据 我想在每个数组中显示名称 下面是gson转换的Pojo类 resresponse.java public class RestResponse { @SerializedName("messages") @Expose private List<String> messages = null; @SerializedName("result") @Expose
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;
}
}
最后重新响应Gson转换器生成的emain.java//Example.java文件
public class RestResponseMain {
@SerializedName("RestResponse")
@Expose
private RestResponse restResponse;
public RestResponse getRestResponse() {
return restResponse;
}
public void setRestResponse(RestResponse restResponse) {
this.restResponse = restResponse;
}
}
通过使用上面的类,我试图通过
实际URL为:
我的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;
}
}
接口是
public interface ApiInterface {
@GET("/country/get/all")
Call<RestResponse> getData();
}
最后,我并没有从上述过程中得到任何反应,我是一个新的改装概念,我尝试通过一些在线教程
根据上述代码中的pojo类向我建议可能的更改。/**
*API调用
*/
私有静态服务接口apiService;
公共服务接口callWSAds(){
如果(!isInternetAvailable()){
}
OkHttpClient.Builder=new OkHttpClient().newBuilder();
builder.readTimeout(HTTP\u超时,TimeUnit.SECONDS)
.writeTimeout(HTTP_超时,时间单位为秒)
.connectTimeout(HTTP_超时,时间单位为秒)
.addInterceptor(新拦截器(){
@凌驾
公共okhttp3.响应截获(链)引发IOException{
Request originalRequest=chain.Request();
Request.Builder requestBuilder=originalRequest.newBuilder().method(originalRequest.method(),originalRequest.body());
Request=requestBuilder.build();
返回链。继续(请求);
}
});
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
建造商.附加接收器(拦截器);
OkHttpClient=builder.build();
改造;
改装=新改装.Builder()
.baseUrl(DEV_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.客户(客户)
.build();
apiService=reformation.create(ServiceInterface.class);
返回服务;
}
长HTTP_超时=80;
callWSAds().getAdsSettingsNew(map).enqueue(新的Callback(){//需要在此处更改
@凌驾
公共void onResponse(调用、响应){
if(response.body()!=null){
saveResponseOffline(response.body());
设置=getResponseOffline();
launchApp();//跟踪应用程序的启动
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
if(设置==null){
loadJSONFromAsset();
}
}
});
此示例包含POST方法调用。请使用这种配置使API调用更简单。您绑定了错误的模型CALS,因此只需更改,并且您的基本url需要以
“/”结尾。
重新响应
到
再反应
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();
}
公共接口{
@获取(“国家/获取/全部”)
调用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) {
}
});
ApiInterface-ApiInterface=ApiClient.getData().create(ApiInterface.class);
Call Call=apiInterface.getData();
pDialog.show();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
pDialog.disclose();
List=response.body().getResult();
MyRecyclerRetrofitAdapter=新的MyRecyclerRetrofitAdapter(MainActivity.this,list);
rView.setAdapter(适配器);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
根据改装文档的规定,您的基本url需要以“/”结尾
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;
}
}
API接口:
public interface ApiInterface {
@GET("country/get/all")
Call<RestResponse> getData();
}
公共接口{
@获取(“国家/获取/全部”)
调用getData();
}
谢谢兄弟列表=response.body().getResponse().getResult();通过这种方式,问题随着您的需求而得到解决suggestion@durgaprasadkalapatiheppy会帮助你的
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) {
}
});
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;
}
}
public interface ApiInterface {
@GET("country/get/all")
Call<RestResponse> getData();
}