1
我正在使用以下URL在一个Android应用程序中从一个URL的10个项目的块中为我的回收站视图获取数据。我无法理解如何更改滚动上的适配器数据集?在Android Recycler中分页查看从URL中获取项目
分页:您可以通过更改API中的开始参数来加载结果的下一页。 例如: 第1页:http://api.smartprix.com/simple/v1?type=search&key=NVgien7bb7P5Gsc8DWqc&category=Mobiles&q=3g&start=0&indent=1 第2页:http://api.smartprix.com/simple/v1?type=search&key=NVgien7bb7P5Gsc8DWqc&category=Mobiles&q=3g&start=10&indent=1
这是我使用从网址得到物品的代码: -
private class FetchProductsTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
// make a HTTP request
response = httpclient.execute(new HttpGet(params[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
Log.d(TAG,"HTTP request status is OK");
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
// close connection
Log.d(TAG,"HTTP request status is close connection");
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (Exception e) {
Log.d(TAG, "Couldn't make a successful request!"+e);
}
return responseString;
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
try {
// convert the String response to a JSON object
Log.d(TAG,"Response = "+response);
JSONObject jsonResponse = new JSONObject(response);
// fetch the array of movies in the response
JSONArray jArray = jsonResponse.getJSONObject("request_result").getJSONArray("results");
Log.d(TAG,"jArray = "+jArray.toString());
for (int i = 0; i < jArray.length(); i++) {
Log.d(TAG,"product = "+jArray.get(i));
JSONObject product = jArray.getJSONObject(i);
products.add(new ProductDataForList(product.getString("id"),product.getString("name"),product.getString("img_url"),product.getInt("price")));
/*movieTitles.add(movie.getString("title"));
movieSynopsis.add(movie.getString(movie.getString("synopsis")));
movieImgUrl.add(movie.getString(movie.getString("id")));*/
}
// refresh the ListView
fillProductList(products);
} catch (JSONException e) {
Log.d("Test", "Couldn't successfully parse the JSON response!");
}
}
}
检查[这](https://github.com/MarkoMilos/Paginate) – Nisarg