How to parse a json using retrofit 2.0?

烈酒焚心 提交于 2021-01-28 17:06:24

问题


Am having a doubt on parsing json objects using retrofit my json response will be like this:

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

My doubt is if result from response is 2 it should redirect to next page . How to create pojo for this json response ?


回答1:


Simply,use GsonConverterFactory, before use that you need add this to your gradle file:

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

Let's say you have a Object called LoginResponse and it has a attribute called loginResult:

public class LoginResponse{
    LoginResult loginResult;
}

The LoginResult object define is like this:

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

Then use Retrofit to request:

    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) {
             // ...
          }
        });
    }

When you get the LoginResponse, you can do you work:

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

Reference:

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



回答2:


Use http://www.jsonschema2pojo.org/ to create pojo class easily for your needs. In that set source type as json and Annotation style Gson. Adding yourjson as pojo created from there

     -----------------------------------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;
}

}


来源:https://stackoverflow.com/questions/33644726/how-to-parse-a-json-using-retrofit-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!