Json解析器为doInBackground获取值

时间:2015-04-11 11:09:31

标签: android json list

我有一个Json解析器和doInBackground用于加载更多结果。我的json包含" name"," image"并且," nextpage"细节。 "下一页"是doInBackground中再次解析的网址链接,以获得更多结果。我怎样才能得到" nextpage"链接到我的doInBackground?

private String next = "first url";
private String url ="myurl
 public List<Item> getData(String url) {

        try {

            listArray = new ArrayList<Item>();
            HttpGet httppost = new HttpGet(url);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);
            int status = response.getStatusLine().getStatusCode();

            HttpEntity entity = response.getEntity();
            String data = EntityUtils.toString(entity);


            JSONObject jsono = new JSONObject(data);
            JSONArray jarray = jsono.getJSONArray("data");
            JSONObject page = jsono.getJSONObject("page");


            for (int i = 0; i < jarray.length(); i++) {
                JSONObject object = jarray.getJSONObject(i);

                    objItem = new Item();

                    next = page.getString("nextpage");
                    objItem.setNext(next);

                    objItem.setTitle(object.getString("name"));
                    objItem.setLink(object.getString("image"));


                    listArray.add(objItem);

             }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return listArray;
    }



    @Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    int loadedItems = firstVisibleItem + visibleItemCount;
    if ((loadedItems == totalItemCount) && !isloading) {
        if (task != null && (task.getStatus() == AsyncTask.Status.FINISHED)) {
            if (hasMore) {
                url = next;
                task = new MyTask();
                task.execute();
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

这取决于您声明异步任务的方式和位置。但是如果在Activity中声明并调用它,你应该在asynctask中添加一个构造函数来获取一个url。然后将其称为new MyAsyncTask(next).execute();。在此之前,您应该将String next = "first url";作为您的Activity的私有变量。在getData()String next = page.getString("nextpage");更改为next = page.getString("nextpage");。从doInBackGround中删除String url = ///////////How getNext()???