gpt4 book ai didi

Java Bean 返回值为 Null

转载 作者:行者123 更新时间:2023-12-01 09:14:15 30 4
gpt4 key购买 nike

我在 post 请求后返回值出现问题,响应字符串包含请求的 JSON 文本,但 dataBean 为 null ??

   @Override
public wDataBean doInBackground(Void... params) {
wDataBean dataBean = new wDataBean();


try {

Response response = client.newCall(request).execute();
String responseString = response.body().string();
JSONObject jsonObject = new JSONObject(responseString);
Gson gson = new Gson();

dataBean = gson.fromJson(jsonObject.toString(), wDataBean.class);

}
catch (final IOException e)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}

return dataBean;
}

这是响应的示例:

  { "IsOK":true,
"Response":"Logged in successfully",
"MyArray":{ "user":"35",
"email":"email@domain.com",
"img":"https:\/\/www.mywebsite.com\/uploads\/136_image.png",
"fname":"First Name",
"lname":"Last Name",
"myToken":
{
"auth":"xyzxyzyxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz",
"expiry":"1980-01-01 00:00:00"
}
},
"myToken":{}
}

wDataBean.java

public class wDataBean {

private Main1 main;

public Main1 getMain() {return main;}

public void setMain(Main1 main) {this.main = main;}

class Main1 {

boolean IsOK;
String Response;
JSONArray MyArray;
int user;
String email;
String img;
String fname;
String lname;
JSONArray myToken;
String auth;
Date expiry;

public boolean isOK() {return isOK();}
public int getUser() {return user;}
public JSONArray getMyArray() {return MyArray;}
public String getEmail() {return email;}
public String getImg() {return img;}
public String getResponse() {return Response;}
public Date getExpiry() {return expiry;}
public JSONArray getMyToken() {return myToken;}
public String getAuth() {return auth;}
public String getFname() {return fname;}
public String getLname() {return lname;}
public void setAuth(String auth) {this.auth = auth;}
public void setEmail(String email) {this.email = email;}
public void setExpiry(Date expiry) {this.expiry = expiry;}
public void setFname(String fname) {this.fname = fname;}
public void setImg(String img) {this.img = img;}
public void setLname(String lname) {this.lname = lname;}
public void setMyArray(JSONArray myArray) {MyArray = myArray;}
public void setMyToken(JSONArray myToken) {this.myToken = myToken;}
public void setOK(boolean OK) {IsOK = OK;}
public void setResponse(String response) { Response = response;}
public void setUser(int user) { this.user = user;}
}

}

目标是从myToken获取“auth”“expiry”

最佳答案

这应该是你的 Bean 类 -

 public class WDataBean {

@SerializedName("IsOK")
@Expose
private Boolean isOK;
@SerializedName("Response")
@Expose
private String response;
@SerializedName("MyArray")
@Expose
private MyArray myArray;
@SerializedName("myToken")
@Expose
private MyToken_ myToken;

/**
*
* @return
* The isOK
*/
public Boolean getIsOK() {
return isOK;
}

/**
*
* @param isOK
* The IsOK
*/
public void setIsOK(Boolean isOK) {
this.isOK = isOK;
}

/**
*
* @return
* The response
*/
public String getResponse() {
return response;
}

/**
*
* @param response
* The Response
*/
public void setResponse(String response) {
this.response = response;
}

/**
*
* @return
* The myArray
*/
public MyArray getMyArray() {
return myArray;
}

/**
*
* @param myArray
* The MyArray
*/
public void setMyArray(MyArray myArray) {
this.myArray = myArray;
}

/**
*
* @return
* The myToken
*/
public MyToken_ getMyToken() {
return myToken;
}

/**
*
* @param myToken
* The myToken
*/
public void setMyToken(MyToken_ myToken) {
this.myToken = myToken;
}

public class MyToken_ {


}
public class MyToken {

@SerializedName("auth")
@Expose
private String auth;
@SerializedName("expiry")
@Expose
private String expiry;

/**
*
* @return
* The auth
*/
public String getAuth() {
return auth;
}

/**
*
* @param auth
* The auth
*/
public void setAuth(String auth) {
this.auth = auth;
}

/**
*
* @return
* The expiry
*/
public String getExpiry() {
return expiry;
}

/**
*
* @param expiry
* The expiry
*/
public void setExpiry(String expiry) {
this.expiry = expiry;
}

}
public class MyArray {

@SerializedName("user")
@Expose
private String user;
@SerializedName("email")
@Expose
private String email;
@SerializedName("img")
@Expose
private String img;
@SerializedName("fname")
@Expose
private String fname;
@SerializedName("lname")
@Expose
private String lname;
@SerializedName("myToken")
@Expose
private MyToken myToken;

/**
*
* @return
* The user
*/
public String getUser() {
return user;
}

/**
*
* @param user
* The user
*/
public void setUser(String user) {
this.user = user;
}

/**
*
* @return
* The email
*/
public String getEmail() {
return email;
}

/**
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}

/**
*
* @return
* The img
*/
public String getImg() {
return img;
}

/**
*
* @param img
* The img
*/
public void setImg(String img) {
this.img = img;
}

/**
*
* @return
* The fname
*/
public String getFname() {
return fname;
}

/**
*
* @param fname
* The fname
*/
public void setFname(String fname) {
this.fname = fname;
}

/**
*
* @return
* The lname
*/
public String getLname() {
return lname;
}

/**
*
* @param lname
* The lname
*/
public void setLname(String lname) {
this.lname = lname;
}

/**
*
* @return
* The myToken
*/
public MyToken getMyToken() {
return myToken;
}

/**
*
* @param myToken
* The myToken
*/
public void setMyToken(MyToken myToken) {
this.myToken = myToken;
}

}
}

关于Java Bean 返回值为 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40692985/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com