我正在开发一个电子学习应用程序。但是从JsonObject解析时我遇到了问题。您能给我一种简单易懂的方法吗?
API request
@GET("category")
Observable<ResponseCategory> category(@Header("Authorization") String token);
{ “状态”:是, “类别”:[ { “ category_id”:“ 1”, “ category_name”:“印尼语”, “ category_detail”:null, “ category_thumbnail”:“ 0” }, { “ category_id”:“ 2”, “ category_name”:“印度语”, “ category_detail”:null, “ category_thumbnail”:“ 0” }, { “ category_id”:“ 3”, “ category_name”:“ Matematika”, “ category_detail”:null, “ category_thumbnail”:“ 0” }, { “ category_id”:“ 4”, “ category_name”:“ IPA”, “ category_detail”:null, “ category_thumbnail”:“ 0” } ]}
类别模型。
@SerializedName("category_detail")
private String mCategoryDetail;
@SerializedName("category_id")
private String mCategoryId;
@SerializedName("category_name")
private String mCategoryName;
@SerializedName("category_thumbnail")
private String mCategoryThumbnail;
public String getCategoryDetail() {
return (String) mCategoryDetail;
}
public void setCategoryDetail(String categoryDetail) {
mCategoryDetail = categoryDetail;
}
public String getCategoryId() {
return mCategoryId;
}
public void setCategoryId(String categoryId) {
mCategoryId = categoryId;
}
public String getCategoryName() {
return mCategoryName;
}
public void setCategoryName(String categoryName) {
mCategoryName = categoryName;
}
public String getCategoryThumbnail() {
return mCategoryThumbnail;
}
public void setCategoryThumbnail(String categoryThumbnail) {
mCategoryThumbnail = categoryThumbnail;
}
ResponseCategory模型
@SerializedName("category")
private List<Category> mCategory;
@SerializedName("status")
private Boolean mStatus;
public List<Category> getCategory() {
return mCategory;
}
public void setCategory(List<Category> category) {
mCategory = category;
}
public Boolean getStatus() {
return mStatus;
}
public void setStatus(Boolean status) {
mStatus = status;
}
从类别中获取可观察到的响应
private Observable<ResponseCategory> loadCategoryData(){
RequestAPI requestAPI = NetworkClient.getRetrofit().create(RequestAPI.class);
return requestAPI.category(user.get(SessionManagement.key_token))
.onErrorResumeNext(throwable -> {
handleCategoryError(throwable);
return null;
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}
记录HTTP请求和响应
D/OkHttp: --> GET http://192.xxx.xxx.x/Elearning-Mobile-API/category
D/OkHttp: Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjIifQ.Ntdca3475YkDSWoiYM72FQhcJr3o8VZu6EMac-4JZZg
D/OkHttp: --> END GET
D/OkHttp: --> GET http://192.xxx.xxx.x/Elearning-Mobile-API/package/latest
D/OkHttp: Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjIifQ.Ntdca3475YkDSWoiYM72FQhcJr3o8VZu6EMac-4JZZg
D/OkHttp: --> END GET
D/OkHttp: --> GET http://192.xxx.xxx.x/Elearning-Mobile-API/package/history?user=8
D/OkHttp: Authorization: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjIifQ.Ntdca3475YkDSWoiYM72FQhcJr3o8VZu6EMac-4JZZg
D/OkHttp: --> END GET
I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/OpenGLRenderer: Swap behavior 0
D/EGL_emulation: eglCreateContext: 0xe5604240: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xe5604240: ver 2 0 (tinfo 0xe5603310)
W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1
I/chatty: uid=10080(com.rakaadinugroho.msuryanusatara) identical 1 line
W/StaticLayout: maxLineHeight should not be -1. maxLines:1 lineCount:1
I/zygote: Do partial code cache collection, code=15KB, data=30KB
D/OkHttp: <-- 200 OK http://192.xxx.xxx.x/Elearning-Mobile-API/category (317ms)
D/OkHttp: Date: Thu, 19 Dec 2019 16:08:14 GMT
D/OkHttp: Server: Apache/2.4.34 (Unix) OpenSSL/1.0.2p PHP/7.2.10 mod_perl/2.0.8-dev Perl/v5.16.3
D/OkHttp: X-Powered-By: PHP/7.2.10
D/OkHttp: Set-Cookie: ci_session=323db33987f1053c406e72a3d848f8ee; expires=Thu, 19-Dec-2019 18:08:14 GMT; Max-Age=7200; path=/; HttpOnly
D/OkHttp: Expires: Thu, 19 Nov 1981 08:52:00 GMT
D/OkHttp: Cache-Control: no-store, no-cache, must-revalidate
D/OkHttp: Pragma: no-cache
D/OkHttp: Content-Length: 3751
D/OkHttp: Keep-Alive: timeout=5, max=100
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: <-- 200 OK http://192.xxx.xxx.x/Elearning-Mobile-API/package/history?user=8 (303ms)
D/OkHttp: Date: Thu, 19 Dec 2019 16:08:14 GMT
D/OkHttp: Server: Apache/2.4.34 (Unix) OpenSSL/1.0.2p PHP/7.2.10 mod_perl/2.0.8-dev Perl/v5.16.3
D/OkHttp: X-Powered-By: PHP/7.2.10
D/OkHttp: Set-Cookie: ci_session=ab5cf498379d7a76bf81bb6cb1bda6a3; expires=Thu, 19-Dec-2019 18:08:14 GMT; Max-Age=7200; path=/; HttpOnly
D/OkHttp: Expires: Thu, 19 Nov 1981 08:52:00 GMT
D/OkHttp: Cache-Control: no-store, no-cache, must-revalidate
D/OkHttp: Pragma: no-cache
D/OkHttp: Content-Length: 58
D/OkHttp: Keep-Alive: timeout=5, max=100
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: <-- 200 OK http://192.168.122.1/Elearning-Mobile-API/package/latest (322ms)
D/OkHttp: Date: Thu, 19 Dec 2019 16:08:14 GMT
D/OkHttp: Server: Apache/2.4.34 (Unix) OpenSSL/1.0.2p PHP/7.2.10 mod_perl/2.0.8-dev Perl/v5.16.3
D/OkHttp: X-Powered-By: PHP/7.2.10
D/OkHttp: Set-Cookie: ci_session=be900455184b37388da130a9d54ee62b; expires=Thu, 19-Dec-2019 18:08:14 GMT; Max-Age=7200; path=/; HttpOnly
D/OkHttp: Expires: Thu, 19 Nov 1981 08:52:00 GMT
D/OkHttp: Cache-Control: no-store, no-cache, must-revalidate
D/OkHttp: Pragma: no-cache
D/OkHttp: Content-Length: 66
D/OkHttp: Keep-Alive: timeout=5, max=100
D/OkHttp: Connection: Keep-Alive
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: {"status":true,"message":"Daftar Ujian Saya","history":[]}
D/OkHttp: <-- END HTTP (58-byte body)
E/RecyclerView: No adapter attached; skipping layout
I/chatty: uid=10080(com.rakaadinugroho.msuryanusatara) identical 1 line
E/RecyclerView: No adapter attached; skipping layout
D/OkHttp: {"status":true,"message":"Latest Examination","package_latest":[]}
D/OkHttp: <-- END HTTP (66-byte body)
I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread.
D/OkHttp: <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
D/OkHttp: <h4>A PHP Error was encountered</h4>
D/OkHttp: <p>Severity: Notice</p>
D/OkHttp: <p>Message: Undefined property: stdClass::$detail</p>
D/OkHttp: <p>Filename: controllers/Category.php</p>
D/OkHttp: <p>Line Number: 26</p>
D/OkHttp: <p>Backtrace:</p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/controllers/Category.php<br />
D/OkHttp: Line: 26<br />
D/OkHttp: Function: _error_handler </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/libraries/REST_Controller.php<br />
D/OkHttp: Line: 708<br />
D/OkHttp: Function: index_get </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/index.php<br />
D/OkHttp: Line: 315<br />
D/OkHttp: Function: require_once </p>
D/OkHttp:
D/OkHttp:
D/OkHttp: </div>
D/OkHttp: <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
D/OkHttp: <h4>A PHP Error was encountered</h4>
D/OkHttp: <p>Severity: Notice</p>
D/OkHttp: <p>Message: Undefined property: stdClass::$detail</p>
D/OkHttp: <p>Filename: controllers/Category.php</p>
D/OkHttp: <p>Line Number: 26</p>
D/OkHttp: <p>Backtrace:</p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/controllers/Category.php<br />
D/OkHttp: Line: 26<br />
D/OkHttp: Function: _error_handler </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/libraries/REST_Controller.php<br />
D/OkHttp: Line: 708<br />
D/OkHttp: Function: index_get </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/index.php<br />
D/OkHttp: Line: 315<br />
D/OkHttp: Function: require_once </p>
D/OkHttp:
D/OkHttp:
D/OkHttp: </div>
D/OkHttp: <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
D/OkHttp: <h4>A PHP Error was encountered</h4>
D/OkHttp: <p>Severity: Notice</p>
D/OkHttp: <p>Message: Undefined property: stdClass::$detail</p>
D/OkHttp: <p>Filename: controllers/Category.php</p>
D/OkHttp: <p>Line Number: 26</p>
D/OkHttp: <p>Backtrace:</p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/controllers/Category.php<br />
D/OkHttp: Line: 26<br />
D/OkHttp: Function: _error_handler </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/libraries/REST_Controller.php<br />
D/OkHttp: Line: 708<br />
D/OkHttp: Function: index_get </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/index.php<br />
D/OkHttp: Line: 315<br />
D/OkHttp: Function: require_once </p>
D/OkHttp:
D/OkHttp:
D/OkHttp: </div>
D/OkHttp: <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
D/OkHttp: <h4>A PHP Error was encountered</h4>
D/OkHttp: <p>Severity: Notice</p>
D/OkHttp: <p>Message: Undefined property: stdClass::$detail</p>
D/OkHttp: <p>Filename: controllers/Category.php</p>
D/OkHttp: <p>Line Number: 26</p>
D/OkHttp: <p>Backtrace:</p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/controllers/Category.php<br />
D/OkHttp: Line: 26<br />
D/OkHttp: Function: _error_handler </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/application/libraries/REST_Controller.php<br />
D/OkHttp: Line: 708<br />
D/OkHttp: Function: index_get </p>
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp:
D/OkHttp: <p style="margin-left:10px">
D/OkHttp: File: /opt/lampp/htdocs/Elearning-Mobile-API/index.php<br />
D/OkHttp: Line: 315<br />
D/OkHttp: Function: require_once </p>
D/OkHttp:
D/OkHttp:
D/OkHttp: </div>{"status":true,"category":[{"category_id":"1","category_name":"Bahasa Indonesia","category_detail":null,"category_thumbnail":"0"},{"category_id":"2","category_name":"Bahasa Inggris","category_detail":null,"category_thumbnail":"0"},{"category_id":"3","category_name":"Matematika","category_detail":null,"category_thumbnail":"0"},{"category_id":"4","category_name":"IPA","category_detail":null,"category_thumbnail":"0"}]}
D/OkHttp: <-- END HTTP (3751-byte body)
答案 0 :(得分:0)
您的响应对象必须像这样。
public class ResponseCategory
{
private Category[] category;
private String status;
public Category[] getCategory ()
{
return category;
}
public void setCategory (Category[] category)
{
this.category = category;
}
public String getStatus ()
{
return status;
}
public void setStatus (String status)
{
this.status = status;
}
@Override
public String toString()
{
return "ClassPojo [category = "+category+", status = "+status+"]";
}
}
public class Category
{
private String category_name;
private String category_id;
private null category_detail;
private String category_thumbnail;
public String getCategory_name ()
{
return category_name;
}
public void setCategory_name (String category_name)
{
this.category_name = category_name;
}
public String getCategory_id ()
{
return category_id;
}
public void setCategory_id (String category_id)
{
this.category_id = category_id;
}
public null getCategory_detail ()
{
return category_detail;
}
public void setCategory_detail (null category_detail)
{
this.category_detail = category_detail;
}
public String getCategory_thumbnail ()
{
return category_thumbnail;
}
public void setCategory_thumbnail (String category_thumbnail)
{
this.category_thumbnail = category_thumbnail;
}
@Override
public String toString()
{
return "ClassPojo [category_name = "+category_name+", category_id = "+category_id+", category_detail = "+category_detail+", category_thumbnail = "+category_thumbnail+"]";
}
}