我正在使用WordPress REST API,因为当用户不从WordPress后端上传图像时,它不包含该字段,并且无法进行类似org.json.JSONException的异常改正:当用户上传图像时,缩略图没有值当响应没有缩略图字段
时,正在获得有关如何处理此问题的响应 ApiInterface apiInterface = RetrofitManager.getclient().create(ApiInterface.class);
Call<JsonObject> call = apiInterface.getPost(intvalue);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call,Response<JsonObject> response) {
if (response.isSuccessful()) {
response.body();
try {
jsonObject = new JSONObject(response.body().toString());
Log.d("@@response",response.body().toString());
JSONObject jsonResult = jsonObject.getJSONObject("post");
String title = jsonResult.getString("title");
String expert = jsonResult.getString("excerpt");
if ( jsonResult.getString("thumbnail")== null) {
Toast.makeText(getapplicationContext(),"No Image Exist",Toast.LENGTH_SHORT).show();
Glide.with(getapplicationContext()).load(R.drawable.sikarlogo).into(imageView);
} else {
imageUrl = jsonResult.getString("thumbnail");
Glide.with(getapplicationContext()).load(imageUrl).into(imageView);
}
tvContent.setText(expert);
tvTitle.setText(title);
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("@Success",response.message());
} else {
Log.e("@NO Success",response.message());
}
}
@Override
public void onFailure(Call<JsonObject> call,Throwable t) {
Log.d("@counselingError","");
}
});
以及我得到的类似答复:
{
"status": "ok","post": {
"id": 16,"type": "post","slug": "%e0%a4%95%e0%a4%bf%e0%a4%a1%e0%a4%bc%e0%a4%9c%e0%a4%bc%e0%a5%80-%e0%a4%b8%e0%a5%80%e0%a4%95%e0%a4%b0-%e0%a4%ae%e0%a5%87%e0%a4%82-%e0%a4%ae%e0%a4%a8%e0%a4%be%e0%a4%af%e0%a4%be-%e0%a4%ac%e0%a4%be","url": "https://sikarsandesh.com/index.php/2019/11/19/%e0%a4%95%e0%a4%bf%e0%a4%a1%e0%a4%bc%e0%a4%9c%e0%a4%bc%e0%a5%80-%e0%a4%b8%e0%a5%80%e0%a4%95%e0%a4%b0-%e0%a4%ae%e0%a5%87%e0%a4%82-%e0%a4%ae%e0%a4%a8%e0%a4%be%e0%a4%af%e0%a4%be-%e0%a4%ac%e0%a4%be/","status": "publish","title": "किड़ज़ी सीकर में मनाया बालदिवस।","title_plain": "किड़ज़ी सीकर में मनाया बालदिवस।","content": "","excerpt": "सीकर के किड़ज़ी स्कूल में बाल दिवस को हर्षोल्लास के साथ मनाया गया। इस अवसर पर बच्चों ने विभिन्न मनोरंजक और खेल-कूद की गतिविधियों में भाग लिया जैसे म्यूज़िकल चेयर,बलून रेस,जलेबी रेस,फ़्री डाँस आदि। बच्चों को स्कूल की और से उपहार भी भेंट किए गए तथा उन्हें पंडित जवाहरलाल नेहरू के जीवन …","date": "2019-11-19 10:32:55","modified": "2019-11-19 10:34:46","categories": [],"tags": [],"author": {
"id": 2,"slug": "adminsikarsandesh-com","name": "admin@sikarsandesh.com","first_name": "","last_name": "","nickname": "admin@sikarsandesh.com","url": "","description": ""
},"comments": [],"attachments": [
{
"id": 17,"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg.jpg","slug": "sikarimg","title": "sikarimg","description": "","caption": "","parent": 16,"mime_type": "image/jpeg","images": {
"full": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg.jpg","width": 960,"height": 640
},"thumbnail": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-150x150.jpg","width": 150,"height": 150
},"medium": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-300x200.jpg","width": 300,"height": 200
},"medium_large": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-768x512.jpg","width": 768,"height": 512
},"woohoo-small": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-104x74.jpg","width": 104,"height": 74
},"woohoo-large": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-850x491.jpg","width": 850,"height": 491
},"woohoo-full": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-960x540.jpg","height": 540
},"woohoo-widget": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-320x220.jpg","width": 320,"height": 220
},"bdaia-carousel": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-309x330.jpg","width": 309,"height": 330
},"bdaia-gallery-grid": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-850x567.jpg","height": 567
},"bdaia-gr1": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-742x490.jpg","width": 742,"height": 490
},"bdaia-gr2": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-496x244.jpg","width": 496,"height": 244
},"bdaia-gr3": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-618x260.jpg","width": 618,"height": 260
},"bdaia-gr4": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-413x244.jpg","width": 413,"bdaia-small": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-104x74.jpg","bdaia-large": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-850x491.jpg","bdaia-full": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-960x540.jpg","bdaia-widget": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-320x220.jpg","bdaia-block11": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-384x220.jpg","width": 384,"woocommerce_thumbnail": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-300x300.jpg","height": 300
},"woocommerce_single": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-600x400.jpg","width": 600,"height": 400
},"woocommerce_gallery_thumbnail": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-100x100.jpg","width": 100,"height": 100
},"shop_catalog": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-300x300.jpg","shop_single": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-600x400.jpg","shop_thumbnail": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-100x100.jpg","height": 100
}
}
}
],"comment_count": 0,"comment_status": "open","thumbnail": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-150x150.jpg","shop_thumbnail": {
"url": "https://sikarsandesh.com/wp-content/uploads/2019/11/sikarimg-100x100.jpg","height": 100
}
}
},
}
在某些情况下,我没有获得如何处理此图像字段的缩略图字段
最后我已经做到了:
if ( jsonResult.has("thumbnail")) {
imageUrl = jsonResult.getString("thumbnail");
Glide.with(getapplicationContext()).load(imageUrl).into(imageView);
} else {
Toast.makeText(getapplicationContext(),Toast.LENGTH_SHORT).show();
Glide.with(getapplicationContext()).load(R.drawable.sikarlogo).into(imageView);
}