java - 改造响应和适当的 POJO(普通旧 Java 对象)

标签 java android retrofit pojo

我调用服务部门,得到的答复如下:

    [
    {
        "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) {

        }
    });
}

我的问题:我的对象类有问题吗,或者是通过服务调用还是......?

最佳答案

我使用 List 回调进行数组响应,效果很好。

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

关于java - 改造响应和适当的 POJO(普通旧 Java 对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131387/

相关文章:

java - 在变量值之后命名 json 属性

android - 上传视频到 Facebook

java - DBCP 数据源池?

java - com.calculator 无法解析为类型

java - 在我的响应中获取 302 重定向的 HttpException

android - 带有改造 API 的 MalformedJsonException?

java.lang.IllegalArgumentException : FormUrlEncoded can only be specified on HTTP methods with request body (e. g., @POST)

java - 我在 Retrofit 中得到奇怪的响应,协议(protocol)=http/1.1,代码=200

java - 如何从 Number 值反序列化为 int(错误 - 没有 int/Int 参数构造函数/工厂方法从 Number 值反序列化)

android - 如何根据另一个小部件中的操作更新 UI 或更改小部件中的状态?