[英]JSON of 10 posts from wp json rest api v1 For Android App
從WordPress網站,使用json rest api,我們獲得整個網站的json。 我想從所有類別的Android加載前10個帖子的json。 我正在使用凌空來加載json數組,並且它無法加載整個數組響應,因為它的大小很大。 我想要前10個帖子,單擊加載更多時,我希望將第11個帖子的json轉換為第20個帖子。 我可以這樣做嗎?
目前,我的網址類似於http://www.example.com/wp-json/posts
我要求以下代碼
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET,
baseUrl,
(String) null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("json",response.toString());
listRecentPost = parseJsonResponse(response);
Log.d("LSN", listRecentPost.isEmpty() + "");
// If any data is avialable
if (!listRecentPost.isEmpty()) {
postAdapter.setRecentPost(listRecentPost);
postAdapter.notifyDataSetChanged();
/*
suppose data connection is off so error image and text will show
* but when my connection will be okk then i need to disable this image and error text
* */
errorImage.setVisibility(View.GONE);
errorMsg.setVisibility(View.GONE);
}
else {
errorMsg.setVisibility(View.VISIBLE);
errorMsg.setText("No Post Available");
}
//disable loading icon
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("LSN", error.toString() + "VolleyError");
// enable error iamge and text
errorImage.setVisibility(View.VISIBLE);
errorMsg.setVisibility(View.VISIBLE);
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
errorMsg.setText(error.toString());
} else if (error instanceof AuthFailureError) {
errorMsg.setText(error.toString());
} else if (error instanceof ServerError) {
errorMsg.setText(error.toString());
} else if (error instanceof NetworkError) {
errorMsg.setText(error.toString());
} else if (error instanceof ParseError) {
errorMsg.setText(error.toString());
}
//again try to load data after 30 seconds
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);
swapeRefresh();
}
}, 30000);
}
});
目前,我遇到了類似com.android.volley.TimeoutError的異常
在JsonArrayRequest中,requestBody參數的作用是什么? 為什么在這里使用null?
用代碼解釋對我來說會更好。
提前致謝。
為什么在那里使用null? 因為您的請求使用的是HTTP方法“ Get”,該方法通過URL傳遞參數。 如果您使用HTTP方法“ Post”,則將請求數據放入該JSONObject中,以便齊射可以將其追加到HTTP-Body-Data中。
您可以嘗試設置自定義RetryPolicy,因為Volley在等待響應4秒后超時- 參見此處
利用所提供的信息很難確定確切的問題。 我看不到您的請求本身有問題。
拍攝為藍色:參數3 (String) null
,可能傳入一個空的json對象。
我絕對會嘗試通過將其設置為通過adb來詳細了解凌空:
adb -s shell setprop log.tag.Volley VERBOSE
這樣,凌空便會在LogCat中提供更多的調試信息。
祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.