如何在url android中基于分页获得响应?

时间:2014-10-25 13:43:04

标签: android android-asynctask androidhttpclient

我有一个网址

例如:http://movie.org/movie/popular?api_key=c68&page=1

现在我能够单独显示第1页,我的问题是如何在滚动时更改页面并在列表视图中显示。现在我能够显示第一页。但我无法继续增加页面并使用它。

我的代码如下:

 int page=1; // i don't know where to add increment of this page and display. 

private class MovieTop extends AsyncTask {

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            Object... params) {
        try {
            return displayTopMovies();
        } catch (IOException e) {
            return null;
        }
    }


    public ArrayList<HashMap<String, String>> displayTopMovies()
            throws IOException {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder
                .append("https://movie.org/movie/popular?");
        stringBuilder.append("?api_key=" + "c68"+"&&"+page);
     // right now i am displaying for page 1 alone. I have issue where to add page++ and use it.
        URL url = new URL(stringBuilder.toString());

        InputStream stream = null;
        try {
            // Establish a connection
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.addRequestProperty("Accept", "application/json");              conn.setDoInput(true);
            conn.connect();
            int responseCode = conn.getResponseCode();
            Log.d(DEBUG_TAG, "The response code is: " + responseCode + " "
                    + conn.getResponseMessage());

            stream = conn.getInputStream();
            return parseTopMovies(stringify(stream));
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }
      parsing been done here..
    private ArrayList<HashMap<String, String>> parseTopMovies(String result) {
        String streamAsString = result;
        ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
        try {
            JSONObject jsonObject = new JSONObject(streamAsString);
            JSONArray array = (JSONArray) jsonObject.get("results");
            for (int i = 0; i < array.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonMovieObject = array.getJSONObject(i);
                map.put(KEY_TITLE,
                        jsonMovieObject.getString("original_title"));
                results.add(map);
            }
        } catch (JSONException e) {
            System.err.println(e);
            Log.d(DEBUG_TAG, "Error parsing JSON. String was: "
                    + streamAsString);
        }
        return results;
    }

    public String stringify(InputStream stream) throws IOException,
            UnsupportedEncodingException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(reader);
        return bufferedReader.readLine();
    }
}
      @Override
    protected void onPostExecute(Object result) {
        update2((ArrayList<HashMap<String, String>>) result);
    };

 here i am displaying result
  public void update2(ArrayList<HashMap<String, String>> result) {

    ListView listView =(ListView)findViewById(R.id.container);

    // Add results to listView.
    adapter = new UpcomingMovieAdapters(this, R.layout.upcoming,result);
    listView.setAdapter(adapter);
    // Update Activity to show listView
    //setContentView(listView);

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

当列表滚动到最后时,将页码更改为下一个数字,然后获取数据并更新列表视图。要检测列表视图的结尾,请使用http://stackoverflow.com/questions/6358428/implementation-of-onscrolllistener-to-detect-the-end-of-scrolling-in-a-listview。然后将页码更改为更高的数字,然后解析它..将其设置为适配器。在适配器上调用notifyDataSetChagned():0