【问题标题】:retrofit response and proper POJO ( plain Old Java Object)改造响应和正确的 POJO(普通的旧 Java 对象)
【发布时间】:2015-09-16 20:14:42
【问题描述】:

我打电话给服务,响应是这样的:

    [
    {
        "CreateDate": null,
        "Creator": 0,
        "Id": 1,
        "LastModifiedDate": null,
        "Modifier": 0,
        "Description": null,
        "Name": "test0"
    },
    {
        "CreateDate": null,
        "Creator": 0,
        "Id": 2,
        "LastModifiedDate": null,
        "Modifier": 0,
        "Description": null,
        "Name": "test2"
    },
    {
        "CreateDate": null,
        "Creator": 1,
        "Id": 3,
        "LastModifiedDate": null,
        "Modifier": 1,
        "Description": null,
        "Name": "test1"
    }
]

我想为此创建适当的对象,所以我创建了两个类,如下所示:

documentListWrapper

public class DocumentListWrapper {
List<DocumentList> documentListList;

}

文档列表

public class DocumentList {
    //others field not needed
    int Id;
    String Name;

}

但是当我调用服务总是失败并转到我的服务的失败方法时,它会抛出:

 D/Retrofit﹕ Content-Type: application/json; charset=utf-8
 D/Retrofit﹕ Server: Microsoft-IIS/8.5
 D/Retrofit﹕ X-Powered-By: ASP.NET
 D/Retrofit﹕ Date: Tue, 30 Jun 2015 06:33:26 GMT
 D/Retrofit﹕ Content-Length: 355
 D/Retrofit﹕ Connection: Keep-Alive
 D/Retrofit﹕ [{"CreateDate":null,"Creator":0,"Id":1,"LastModifiedDate":null,"Modifier":0,"Description":null,"Name":"test0"},{"CreateDate":null,"Creator":0,"Id":2,"LastModifiedDate":null,"Modifier":0,"Description":null,"Name":"test1"},{"CreateDate":null,"Creator":1,"Id":3,"LastModifiedDate":null,"Modifier":1,"Description":null,"Name":"test2"}]
D/Retrofit﹕ <--- END HTTP (355-byte body)

**已编辑:**

服务:

 @GET("/DocumentTypeList1")
void documentTypeList1(
        @Query("userName") String userName,
        @Query("password") String password, Callback<DocumentListWrapper> callback);

serviceHelper 类:

        public static final int TIMEOUT_CONNECTION = 6000;
    public static final int TIMEOUT_SOCKET = 30000;

    private static ServiceHelper instance = new ServiceHelper();
    RestAdapter restAdapter;
    IocService service;

    private ServiceHelper() {
        restAdapter = createAdapter();
        service = restAdapter.create(IocService.class);
    }

    public static ServiceHelper getInstance() {
        return instance;
    }

    private RestAdapter createAdapter() {

        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used.
        HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

        return new RestAdapter.Builder()
                .setEndpoint(ENDPOINT)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setClient(new ApacheClient(httpClient))
                .build();
    }
}

public void documentTypeList1(String userName,String password,Callback<DocumentListWrapper> callback){
        service.documentTypeList1(userName,password,callback);

我用这样的方法调用服务:

public void documentListType(){
    ServiceHelper.getInstance().documentTypeList1("userName", "password", new Callback<DocumentList>() {
        @Override
        public void success(DocumentListWrapper documentListWrapper, Response response) {

        }

        @Override
        public void failure(RetrofitError retrofitError) {

        }
    });
}

我的问题:我的 Object 类是否有问题,或者是服务调用或...?

【问题讨论】:

  • 看来你的 Object 类没问题。你能显示改造电话吗?
  • @nicopasso 谢谢,看我编辑的问题:)

标签: java android retrofit pojo


【解决方案1】:

我正在使用List 回调进行数组响应,它工作正常。

@GET("/DocumentTypeList1")
void documentTypeList1(
        @Query("userName") String userName,
        @Query("password") String password, Callback<List<DocumentList>> callback);

【讨论】:

  • 我的 documentListWrapper 是不是也一样?
  • idk,但也许,如果 DocumentListWrapper 有效,json 看起来像 { "documentListList": [...]}
猜你喜欢
  • 2011-03-20
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 2022-07-06
  • 2011-07-25
  • 2019-06-02
相关资源
最近更新 更多