我正在开发一个电子学习应用程序。但是我在从JsonObject解析时遇到了一个问题。你能给我一个简单易懂的方法来做这件事吗?
API request
@GET("category")
Observable<ResponseCategory> category(@Header("Authorization") String token);
{"状态":true,"类别": [ { "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;
}
响应类别模型
@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)
你的回应对象应该是这样的。
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+"]";
}
}
我收到了firebase函数和dialogflow的响应,其中包含了我进一步实现所需的一些信息。响应是一个JSON,我将其保存在中,它是一个HashMap(字符串,对象)类型的变量。 我搜索过其他类似的线程,但奇怪的是,我的问题出现在第2列,而不是第1列,我在json中没有看到问题。这是gson部分。错误被抛出到带有属性的行上! 这是回应JSON:
我正在学习烤肉,但在尝试时遇到了一个问题。发现了很多类似的问题和更多的解决方案,但没有任何帮助 这是我的错 游戏者班 以及我是如何尝试着 紧急求救信号
在尝试将json文本文件解析为餐厅对象的数组列表时,我遇到了错误“应为BEGIN\u数组,但在第1行第2列路径处为BEGIN\u对象。”我不知道哪里弄错了,因为我的Restaurant类中的数据成员与json文件中的字段完全对应。 主要的 餐厅类 txt文件
问题内容: 我遇到错误了。 由于以下原因,无法解析JSON:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列为BEGIN_OBJECT 服务器网址 执行请求 上课后 我该如何解决? 问题答案: 您在注释中声明返回的JSON是这样的: 您告诉Gson您有一个对象数组:
问题内容: 我知道这不是第一次有人问这个问题,但是有了Retrofit2,我找不到适合我问题的正确解决方案。我遵循了在线教程,效果很好。当我将相同的代码应用于自己的端点时,会出现以下异常:我不知道该如何解决。 接口: 客户代码: 此代码与此有效负载一起使用: 但与此不: 我的音乐课: 问题答案: 当您说“此代码正在使用此有效负载:…而不是与此有效:: …”时,这是预期的,这就是假定的工作方式。实际
我是android编程新手。我有一个类,在这个类中,修改API调用是为了解析和显示JSON文件中的几个属性。但我得到: Java语言lang.IllegalStateException:应为BEGIN\u对象,但在第2行第1列为字符串 好心帮忙。我搜索了答案,但没有发现JSON响应有任何问题。发布了我的JSON响应和JAVA类: JSON响应: JAVA文件: 模型类: