Java 改装发送Json对象和获取列表作为响应

Java 改装发送Json对象和获取列表作为响应,java,android,retrofit2,Java,Android,Retrofit2,我向我的服务发送json对象,如下所示 { "procedure_name": "testowa", "parameters": [ { "Id": "1" } ] } 作为响应,我在同一url上获得sql过程的结果集: [ {"id": 1, "email": "test", "password": "test"} ] 为此,我编写了以下代码。 这是我的api接口: public interface ApiInterface { @PO

我向我的服务发送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);
}
公共接口{
@职位(“/”)
调用getData(@Body 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;
    }
}
公共类程序
{
最终字符串程序名称;
最终清单参数;
公共过程(字符串过程名称,列表参数)
{
this.procedure\u name=procedure\u name;
此参数=参数;
}
}
在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());
        }
    });
ApiInterface服务=改装.create(ApiInterface.class);
HashMap参数=新的HashMap();
参数put(“id”,“1”);
List_parameters=new ArrayList();
列出参数。添加(参数);
程序程序=新程序(“testowa”,列出参数);
Call Call=service.getData(过程);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
字符串apiResponse=new Gson().toJson(response.body());
textViewResult.append(apiResponse);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
textViewResult.setText(t.getMessage());
}
});

我像保存字符串一样保存响应,但我想像保存对象列表一样保存响应。我应该再创建一个类,可能是用户,并将响应保存为该类的对象列表。但是我不知道如何更改我的api接口和其他方法来实现这一点。

我像这样发送到我的服务json对象

{ “程序名称”:“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;
            }
        }
    }
公共类myRequest{
字符串程序名称;
阵列列表参数;
公共字符串getProcedure_name(){
返回程序名称;
}
public void setProcedure_name(字符串procedure_name){
this.procedure\u name=procedure\u name;
}
公共ArrayList getParameters(){
返回参数;
}
公共void setParameters(ArrayList参数){
此参数=参数;
}
公共myRequest(){
}
公共myRequest(字符串过程名称、ArrayList参数){
this.procedure\u name=procedure\u name;
此参数=参数;
}
公共类myObject{
字符串Id;
公共字符串getId(){
返回Id;
}
公共无效集合id(字符串id){
Id=Id;
}
公共对象(){
}
公共myObject(字符串id){
Id=Id;
}
}
}
并将此更改为:

public interface ApiInterface {
    @POST("/")
    Call<Object> getData(@Body myRequest myRequest);
}
公共接口{
@职位(“/”)
调用getData(@Body myRequest myRequest);
}
在您的活动或片段中,执行以下操作以调用:

ArrayList<String> list = new ArrayList<>;
list.add("1");
 myRequest myRequest = new myRequest ("testowa", list);

 Call<Object> call = service.getData(myRequest );
ArrayList list=新的ArrayList;
列表。添加(“1”);
myRequest myRequest=新的myRequest(“testowa”,列表);
Call Call=service.getData(myRequest);

它将起作用。

它的直截了当的“”可以帮助您从Api响应生成POJO。获得生成的POJO后,用POJO类替换Api接口方法中的“Object”类型。您还需要将回调响应类型更改为生成的POJO类,第一步是基于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;
}

}
GsonBuilder gsonBuilder = new GsonBuilder()
Gson gson = gsonBuilder.create();
private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl("apiEndpoint/url")
                .addConverterFactory(GsonConverterFactory.create(gson));
然后,如果您还没有这样做,您应该将Gson构建器传递给您的改造实例创建,如下所示:

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;
}

}
GsonBuilder gsonBuilder = new GsonBuilder()
Gson gson = gsonBuilder.create();
private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl("apiEndpoint/url")
                .addConverterFactory(GsonConverterFactory.create(gson));
然后,您必须相应地调整改造API,以接收与JSOn数组类似的用户列表:

public interface ApiInterface {
@POST("/")
Call<List<User>> getData(@Body Procedure procedure);
}
公共接口{
@职位(“/”)
调用getData(@Body Procedure);
}
最后,改装将以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());
    }
});
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
List apiResponse=response.body();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
textViewResult.setText(t.getMessage());
}
});