簡體   English   中英

如何使用retrofit 2.0解析json?

[英]How to parse a json using retrofit 2.0?

我對使用改造我的json響應解析json對象有疑問將是這樣的:

{ “loginResult”: “{\\” 結果\\ “:2,\\” 用戶名\\ “:0,\\” 的moduleId \\ “:1,\\” 模塊名\\ “:\\” CRM \\ “}”}

我懷疑的是,如果響應的結果是2,它應該重定向到下一頁。 如何為這個json響應創建pojo?

簡單地說,使用GsonConverterFactory ,在使用之前需要將它添加到gradle文件中:

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

假設您有一個名為LoginResponse的Object,它有一個名為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

使用http://www.jsonschema2pojo.org/輕松創建pojo類以滿足您的需求。 在那個集合源類型中為json和Annotation樣式的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;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM