如何返回改造时使用的方法OnResponse的列表?

时间:2017-08-11 12:20:11

标签: android list return retrofit

我想这样做......

  public final List<ClientResponse> returnedList2 = new ArrayList<>();
  clientPost3(nome, returnedList2);

然后调用方法clientPost3,如下所示:

 public void clientPost3(String nome, final List<ClientResponse> returnedList) {

    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(SERVER_URL)
            .build();

    final Interface service = retrofit.create(Interface.class);

    Call<List<ClientResponse>> call = service.postClient(nome);

    call.enqueue(new Callback<List<ClientResponse>>() {
        @Override
        public void onResponse(Call<List<ClientResponse>> call, Response<List<ClientResponse>> response) {
            for(ClientResponse clientResponse: response.body()){
                Log.e(TAG, clientResponse.getCliente());
                returnedList.add(clientResponse);

            }
            Log.e(TAG, "Success");
            Log.e("Teste2", returnedList.toString());
        }

        @Override
        public void onFailure(Call<List<ClientResponse>> call, Throwable t) {
            Log.e(TAG, "FAIL");
        }

    });

}

但是,在调用方法clientPost3之后,returnedList2为空后变为空。

我该怎样做才能获得在onResponse里面的listList列表?

3 个答案:

答案 0 :(得分:0)

你可以这样使用

 public List<ClientResponse> returnedList2 = new ArrayList<>();

 public void clientPost3(String nome, final List<ClientResponse> returnedList) {

Retrofit retrofit = new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl(SERVER_URL)
        .build();

final Interface service = retrofit.create(Interface.class);

Call<List<ClientResponse>> call = service.postClient(nome);

call.enqueue(new Callback<List<ClientResponse>>() {
    @Override
    public void onResponse(Call<List<ClientResponse>> call, Response<List<ClientResponse>> response) {
        returnedList2 = response.body();
        Log.e(TAG, "Success");
        Log.e("Teste2", returnedList.toString());
    }

    @Override
    public void onFailure(Call<List<ClientResponse>> call, Throwable t) {
        Log.e(TAG, "FAIL");
    }

});

}

答案 1 :(得分:0)

试试这个,

    public void clientPost3(String nome, final List<ClientResponse> returnedList) {

        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(SERVER_URL)
                .build();

        final Interface service = retrofit.create(Interface.class);

        Call<List<ClientResponse>> call = service.postClient(nome);

        call.enqueue(new Callback<List<ClientResponse>>() {
            @Override
            public void onResponse(Call<List<ClientResponse>> call, Response<List<ClientResponse>> response) {
                returnedList2.clear();
                returnedList2.addAll( response.body().getClientResponse());

            }

            @Override
            public void onFailure(Call<List<ClientResponse>> call, Throwable t) {
                Log.e(TAG, "FAIL");
            }

        });

    }

答案 2 :(得分:0)

您的ClientResponse字段必须在服务器响应中命名,或者标记为@SerializedName(“”)

例如,来自服务器的响应是在Json中:

{
 "time": "15:29",
 "date": "11.08.2017"
}

然后你的班级必须像这样

class MyClass{
private String time;
private String date;
//getters, setters, stc
}

class MyClass{
@SerializedName("time")
private String cerrentTime;
@SerializedName("date")
private String currentDate;
//getters, setters, etc

然后您的回复将被正确解析。

其他问题可能是你在call.enqueue之后检查你的list2,但是Retrofit是异步的,更好的方法是使用它,有一些回调接口,如:

interface MyInterface{
 void onSuccess(ArrayList<ClientResponse> list);
}

在您的活动,片段或您需要的地方实施此界面。然后将MyInterface引用传递给您调用的类,并在onResponse方法中使用myInterfaceInstanse.onSuccess(response.body());并在onSuccess实现中继续使用此响应。

对不起,对于这样一个困惑的答案,希望你理解我:)。