【问题标题】:How to parse a json using retrofit 2.0?如何使用改造 2.0 解析 json?
【发布时间】:2015-11-11 15:21:55
【问题描述】:

我对使用改造解析 json 对象有疑问 我的 json 响应将是这样的:

{"loginResult":"{\"Result\":2,\"UserID\":0,\"ModuleID\":1,\"ModuleName\":\"CRM\"}"}

我怀疑响应的结果是否为 2,它应该重定向到下一页。如何为这个 json 响应创建 pojo?

【问题讨论】:

    标签: android json retrofit


    【解决方案1】:

    只需使用GsonConverterFactory,在使用之前您需要将其添加到您的 gradle 文件中:

    compile 'com.squareup.retrofit:converter-gson'
    

    假设您有一个名为 LoginResponse 的对象,它有一个名为 loginResult 的属性:

    public class LoginResponse{
        LoginResult loginResult;
    }
    

    LoginResult 对象定义如下:

    public class LoginResult{
        int result;
        long userId;
        ...
    }
    

    然后使用Retrofit请求:

        public interface APIService {
            @POST("SOMETHING/login")
            Call<LoginResponse> doLogin();
    
        }
    
        public void doSomething() {
            Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("YOUR LOGIN BASE URL")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
            APIService service = retrofit.create(APIService.class);
            Call<LoginResponse> loginCall = service.doLogin();
            //if you want to request synchronous:
            LoginResponse response = loginCall.execute();
            //if you want to request asynchronous:
            LoginResponse response = loginCall.enqueue(new Callback<LoginResponse>() {
              @Override void onResponse(/* ... */) {
                   // ...
               }
    
              @Override void onFailure(Throwable t) {
                 // ...
              }
            });
        }
    

    当你得到LoginResponse,你就可以工作了:

    if(response.loginResult.result == 2){
        //do work here.something like startActivity(...);
    }
    

    参考:

    1. https://realm.io/news/droidcon-jake-wharton-simple-http-retrofit-2/
    2. http://inthecheesefactory.com/blog/retrofit-2.0/en

    【讨论】:

    • 我需要创建两个pojo吗
    • 是的。Gson 会自动为你创建对象。
    • 如何调用,如果结果为 2,它将重定向到主异步改造中的下一个活动
    • 如果不想创建POJO,可以将响应类型设置为JSONObject,并进行处理。
    【解决方案2】:

    使用http://www.jsonschema2pojo.org/ 轻松创建 pojo 类以满足您的需求。在该设置源类型为 json 和注释样式 Gson。将 yourjson 添加为从那里创建的 pojo

         -----------------------------------com.example.Example.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("org.jsonschema2pojo")
    public class Example {
    
    @SerializedName("loginResult")
    @Expose
    private LoginResult loginResult;
    
    /**
    * 
    * @return
    * The loginResult
    */
    public LoginResult getLoginResult() {
    return loginResult;
    }
    
    /**
    * 
    * @param loginResult
    * The loginResult
    */
    public void setLoginResult(LoginResult loginResult) {
    this.loginResult = loginResult;
    }
    
    }
    -----------------------------------com.example.LoginResult.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("org.jsonschema2pojo")
    public class LoginResult {
    
    @SerializedName("Result")
    @Expose
    private Integer Result;
    @SerializedName("UserID")
    @Expose
    private Integer UserID;
    @SerializedName("ModuleID")
    @Expose
    private Integer ModuleID;
    @SerializedName("ModuleName")
    @Expose
    private String ModuleName;
    
    /**
    * 
    * @return
    * The Result
    */
    public Integer getResult() {
    return Result;
    }
    
    /**
    * 
    * @param Result
    * The Result
    */
    public void setResult(Integer Result) {
    this.Result = Result;
    }
    
    /**
    * 
    * @return
    * The UserID
    */
    public Integer getUserID() {
    return UserID;
    }
    
    /**
    * 
    * @param UserID
    * The UserID
    */
    public void setUserID(Integer UserID) {
    this.UserID = UserID;
    }
    
    /**
    * 
    * @return
    * The ModuleID
    */
    public Integer getModuleID() {
    return ModuleID;
    }
    
    /**
    * 
    * @param ModuleID
    * The ModuleID
    */
    public void setModuleID(Integer ModuleID) {
    this.ModuleID = ModuleID;
    }
    
    /**
    * 
    * @return
    * The ModuleName
    */
    public String getModuleName() {
    return ModuleName;
    }
    
    /**
    * 
    * @param ModuleName
    * The ModuleName
    */
    public void setModuleName(String ModuleName) {
    this.ModuleName = ModuleName;
    }
    
    }
    

    【讨论】:

    • 我需要创建两个pojo吗
    • 如何调用,如果结果为 2,它将重定向到主异步改造中的下一个活动
    • 您需要为此实现回调。成功请求后,回调的成功方法将执行。在该回调中,您可以重定向到新活动
    • 检查上面的代码。在那个 logincall.enque 方法将执行
    • 我需要为登录结果创建对象然后我需要调用getresult方法还是有任何其他程序
    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多