[英]How to parse a json using retrofit 2.0?
Am having a doubt on parsing json objects using retrofit my json response will be like this: 我对使用改造我的json响应解析json对象有疑问将是这样的:
{"loginResult":"{\\"Result\\":2,\\"UserID\\":0,\\"ModuleID\\":1,\\"ModuleName\\":\\"CRM\\"}"} { “loginResult”: “{\\” 结果\\ “:2,\\” 用户名\\ “:0,\\” 的moduleId \\ “:1,\\” 模块名\\ “:\\” CRM \\ “}”}
My doubt is if result from response is 2 it should redirect to next page . 我怀疑的是,如果响应的结果是2,它应该重定向到下一页。 How to create pojo for this json response ? 如何为这个json响应创建pojo?
Simply,use GsonConverterFactory
, before use that you need add this to your gradle file: 简单地说,使用GsonConverterFactory
,在使用之前需要将它添加到gradle文件中:
compile 'com.squareup.retrofit:converter-gson'
Let's say you have a Object called LoginResponse
and it has a attribute called loginResult
: 假设您有一个名为LoginResponse
的Object,它有一个名为loginResult
的属性:
public class LoginResponse{
LoginResult loginResult;
}
The LoginResult
object define is like this: LoginResult
对象的定义如下:
public class LoginResult{
int result;
long userId;
...
}
Then use Retrofit
to request: 然后使用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) {
// ...
}
});
}
When you get the LoginResponse
, you can do you work: 当你获得LoginResponse
,你可以工作:
if(response.loginResult.result == 2){
//do work here.something like startActivity(...);
}
Reference: 参考:
Use http://www.jsonschema2pojo.org/ to create pojo class easily for your needs. 使用http://www.jsonschema2pojo.org/轻松创建pojo类以满足您的需求。 In that set source type as json and Annotation style Gson. 在那个集合源类型中为json和Annotation样式的Gson。 Adding yourjson as pojo created from there 将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.