我向我的服务json对象发送了这样的信息
{
"procedure_name": "testowa",
"parameters": [
{
"Id": "1"
}
]
}
作为回应,我在同一个url上得到sql过程的结果集。
[
{"id": 1, "email": "test", "password": "test"}
]
为了做到这一点,我写了这段代码。这是我的api接口。
public interface ApiInterface {
@POST("/")
Call<Object> getData(@Body Procedure procedure);
}
这是我的过程类
public class Procedure
{
final String procedure_name;
final List<HashMap<String, String>> parameters;
public Procedure(String procedure_name, List<HashMap<String, String>> parameters)
{
this.procedure_name = procedure_name;
this.parameters = parameters;
}
}
在最后的MainActivity部分,我保存了响应。
ApiInterface service = retrofit.create(ApiInterface.class);
HashMap<String, String> parameter = new HashMap<>();
parameter.put("id", "1");
List<HashMap<String, String>> list_parameters = new ArrayList<>();
list_parameters.add(parameter);
Procedure procedure = new Procedure("testowa", list_parameters);
Call<Object> call = service.getData(procedure);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
String apiResponse = new Gson().toJson(response.body());
textViewResult.append(apiResponse);
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
我把响应保存为字符串,但我想把它保存为对象列表。我应该再创建一个类,也许是User,然后把响应保存为这个类的对象列表。但我不知道如何改变我的api接口和其他方法来做到这一点。
第一步是根据你的API响应创建一个POJO,类似这样。
import com.google.gson.annotations.*;
public class User {
@Expose
private Integer id;
@Expose
private String email;
@Expose
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
然后,如果你还没有做,你应该传递一个Gson builder给你的Retrofit实例创建,就像这样。
GsonBuilder gsonBuilder = new GsonBuilder()
Gson gson = gsonBuilder.create();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl("apiEndpoint/url")
.addConverterFactory(GsonConverterFactory.create(gson));
然后,你必须相应地调整你的 Retrofit API,以接收一个用户列表,就像一个 JSOn 数组一样。
public interface ApiInterface {
@POST("/")
Call<List<User>> getData(@Body Procedure procedure);
}
最后,Retrofit将以POJO形式提供给你响应。
call.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
List<User> apiResponse =response.body();
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
它是直接的http:/jsonschema2pojo.org。'可以帮助您从Api响应中生成POJO。一旦得到生成的POJO,将Api接口方法中的 "对象 "类型替换为POJO类。您还需要将CallBack响应类型改为生成的POJO类。
我向我的服务json对象发送了这样的信息
{ "procedure_name": "testowa", "参数": [ { "Id": "1" }
]}
/ 对于这个请求,你需要使用这个类---。
public class myRequest{
String procedure_name;
ArrayList<myObject> parameters;
public String getProcedure_name() {
return procedure_name;
}
public void setProcedure_name(String procedure_name) {
this.procedure_name = procedure_name;
}
public ArrayList<myObject> getParameters() {
return parameters;
}
public void setParameters(ArrayList<myObject> parameters) {
this.parameters = parameters;
}
public myRequest() {
}
public myRequest(String procedure_name, ArrayList<myObject> parameters) {
this.procedure_name = procedure_name;
this.parameters = parameters;
}
public class myObject{
String Id;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public myObject() {
}
public myObject(String id) {
Id = id;
}
}
}
并将此改为 。
public interface ApiInterface {
@POST("/")
Call<Object> getData(@Body myRequest myRequest);
}
并在你的活动或Fragment中这样调用 :
ArrayList<String> list = new ArrayList<>;
list.add("1");
myRequest myRequest = new myRequest ("testowa", list);
Call<Object> call = service.getData(myRequest );
它将工作 .