gpt4 book ai didi

android - 如何在 Retrofit 中获取事物列表

转载 作者:搜寻专家 更新时间:2023-11-01 09:41:13 25 4
gpt4 key购买 nike

有一些关于 Retrofit 的内容我没有理解。我在网上关注示例,但无法编译。我能够通过旧学校(即 HttpGet/HttpResoponse)从 RESTful 服务访问数据,所以我知道该服务有效。它返回一堆 EmployeeData(与仅一个 EmployeeData 相对)

在应用程序 gradle 文件中:

dependencies {
...
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
...
}

RESTful 服务端点的 url 是: https://com.somewhere/PhoneDirectoryService/api/Employees/

我已经为基本 url 定义了一个字符串:

<string name="https_phone_directory_service_baseurl">https://com.somewherePhoneDirectoryService/api/</string>

界面

public interface EmployeesService {
@GET("Employees.json") // the string in the GET is end part of the endpoint url
public Call<List<EmployeeEndpointResponse>> listEmployees();
}

回应

public class EmployeeEndpointResponse {

private List<EmployeeData> employees; // EmployeeData is a POJO

// public constructor is necessary for collections
public EmployeeEndpointResponse() {
employees = new ArrayList<EmployeeData>();
}

public static EmployeeEndpointResponse parseJSON(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
Type collectionType = new TypeToken<List<EmployeeData>>(){}.getType();
Gson gson = gsonBuilder.create();
EmployeeEndpointResponse employeeEndpointResponse = gson.fromJson(response, EmployeeEndpointResponse.class);
return employeeEndpointResponse;
}
}

获取数据

public static boolean getEmployeeData(Context context) {

Resources resources = context.getResources();
URI uri = null;
try {
uri = new URI(resources.getString(R.string.https_phone_directory_service_baseurl));
}
catch (URISyntaxException exception) {
Log.e("getEmployeeData", exception.toString());
}

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(uri.toString())
.addConverterFactory(GsonConverterFactory.create())
.build();
Call<List<EmployeeEndpointResponse>> call = service.listEmployees();

EmployeesService service = retrofit.create(EmployeesService.class);

// this does not compile
// error: <anonymous com.somewhere.utilities.Utilities$1> is not
// abstract and does not override abstract method
// onFailure(Call<List<EmployeeEndpointResponse>>,Throwable) in Callback
call.enqueue(new Callback<List<EmployeeEndpointResponse>>() {
@Override
public void onResponse(Response<List<EmployeeEndpointResponse>> response) {
List<EmployeeEndpointResponse> myList = response.body();
// Successfull request, do something with the retrieved messages
}

@Override
public void onFailure(Throwable t) {
// Failed request.
}
});

// so I tried this which gives compile error
// retrofit2.Callback<java.util.List
// <com.somewhere.gson.EmployeeEndpointResponse>>)
// in Call cannot be applied to anonymous retrofit2.Callback
// <com.somewhere.gson.EmployeeEndpointResponse>)
call.enqueue(new Callback<EmployeeEndpointResponse>() {
@Override
public void onResponse(Call<EmployeeEndpointResponse> call, Response<EmployeeEndpointResponse> response) {
// handle response here
EmployeeEndpointResponse employeeEndpointResponse = (EmployeeEndpointResponse)response.body();
}

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

}
});

}

我需要做什么?

数据看起来像:

[
{"Name":"Smith, Ken J.",
"Cell":"",
"EmailAddress":"Ken.Smith@somewhere.com",
"Location":"West",
"Phone":"555-555-5555",
"Address":"PO Box 555 5555 Del Norte",
"City":"Jackson",
"State":"WY",
"Zip":"85555",
"Latitude":42.24976,
"Longitude":-107.82171},

{"Name":"Cox, Daniel B.",
"Cell":"",
"EmailAddress":"daniel.cox@somewhere.com",
"Location":"West",
"Phone":"(555) 555-5516",
etc ...}
]

最佳答案

添加依赖

dependencies {
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
}

生成http客户端

private static OkHttpClient getOkHttpClient(){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.NONE);
OkHttpClient okClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
return okClient;
}

获取数据

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(YOUR_URL)
.client(getOkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;


EmployeesService service = retrofit.create(EmployeesService.class);
Call<EmployeeEndpointResponse> call = service.listEmployees();
call.enqueue(new Callback<EmployeeEndpointResponse>() {
@Override
public void onResponse(Call<EmployeeEndpointResponse> call, Response<EmployeeEndpointResponse> response) {
EmployeeEndpointResponse employeeEndpointResponse = response.body();
//manage your response
}

@Override
public void onFailure(Call<EmployeeEndpointResponse> call, Throwable t) {
}
});

解析 JSON

您的 POJO 不需要任何其他方法来解析 JSON。只需使用 http://www.jsonschema2pojo.org/ 生成带有 GSON 注释的代码

关于android - 如何在 Retrofit 中获取事物列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com