Android:AsyncTask从url获取Json

时间:2013-12-01 03:47:26

标签: android json android-asynctask

请帮助我:
使用Android 2.2时,此代码非常有效:(直接在标记为注释的行中调用getJSONFromUrl)。

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                int lastInScreen = firstVisibleItem + visibleItemCount;

                if (onStatupBool == true) {
                    JSONParser jParser = new JSONParser();
                    // JSONObject json = jParser
                    // .getJSONFromUrl(urlTruyenMoiNhat);
                    // RetreiveListStory(json);
                    currentURL = urlTruyenMoiNhat;
                    GetJsonAsync gjas = new GetJsonAsync();
                    gjas.execute();
                    loadMoreOnStartUp = true;
                    onStatupBool = false;
                } else if (lastInScreen == totalItemCount
                        && onStatupBool == false
                        && loadAnotherCategory == false
                        && onStopLoadMore == false) {
                    // flag = false;
                    JSONParser jParser = new JSONParser();
                    String url = "";
                    if (loadMoreOnStartUp) {
                        index++;
                        url = urlTruyenMoiNhat + "&&pageIndex=" + index
                                + "&&pageSize=10";
                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyenmoinhat))) {
                        index++;
                        url = urlTruyenMoiNhat + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoivova))) {
                        index++;
                        url = urlTruyenVova + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if ((categoryStory.equals(getResources()
                            .getString(R.string.truyencuoi18)))) {
                        index++;
                        url = urlTruyen18 + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if ((categoryStory.equals(getResources()
                            .getString(R.string.truyencuoitinhyeu)))) {
                        index++;
                        url = urlTruyenTinhYeu + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoidangian))) {
                        index++;
                        url = urlTruyenDanGian + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoitayduky))) {
                        index++;
                        url = urlTruyenTayDuKy + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyentieulam))) {
                        index++;
                        url = urlTruyenTayDuKy + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyentrangquynh))) {
                        index++;
                        url = urlTruyenTrangQuynh + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoigiadinh))) {
                        index++;
                        url = urlTruyenCuoiGiaDinh + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyenthocacuoi))) {
                        index++;
                        url = urlThoCaCuoi + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoithegioi))) {
                        index++;
                        url = urlTruyenCuoiTheGioi + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoingan))) {
                        index++;
                        url = urlTruyenNgan + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoitamquoc))) {
                        index++;
                        url = urlTruyenCuoiTamQuoc + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoihocsinh))) {
                        index++;
                        url = urlTruyenCuoiHocSinh + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoicongso))) {
                        index++;
                        url = urlTruyenCuoiCongSo + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoiyhoc))) {
                        index++;
                        url = urlTruyenYHoc + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoigiaothong))) {
                        index++;
                        url = urlTruyenCuoiGiaoThong + "&&pageIndex="
                                + index + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoisayxin))) {
                        index++;
                        url = urlTruyenSayXin + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoithethao))) {
                        index++;
                        url = urlTruyenCuoiTheThao + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoiphapluat))) {
                        index++;
                        url = urlTruyenCuoiPhapLuat + "&&pageIndex="
                                + index + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoinhahang))) {
                        index++;
                        url = urlTruyenCuoiNhaHang + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoinhabinh))) {
                        index++;
                        url = urlTruyenCuoiNhaBinh + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    } else if (categoryStory.equals(getResources()
                            .getString(R.string.truyencuoitongiao))) {
                        index++;
                        url = urlTruyenCuoiTonGiao + "&&pageIndex=" + index
                                + "&&pageSize=10";

                    }
                    // JSONObject json = jParser.getJSONFromUrl(url);
                    // RetreiveListStory(json);
                    currentURL = url;
                    GetJsonAsync gjas = new GetJsonAsync();
                    gjas.execute();
                    listLoadMore.setSelection(lastInScreen - 2);
                }
                loadAnotherCategory = false;

            }
        });

但是在3.2。不允许直接调用getJSONFromUrl。所以我使用Asyntask: 正如我在上面的代码块中使用的那样。然后它无法正常工作。

private class GetJsonAsync extends AsyncTask<String, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(String... params) {
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(currentURL);

        RetreiveListStory(json);
        return null;
    }

    @Override
    protected void onPostExecute(JSONObject json) {

    }

}

RetreiveListStory函数:

private void RetreiveListStory(JSONObject json) {
    try {
        // Getting Array of Contacts
        contacts = json.getJSONArray(RESPONSE);

        if (contacts.length() == 0) {
            onStopLoadMore = true;
        }
        // looping through All Contacts
        for (int i = 0; i < contacts.length(); i++) {
            JSONObject c = contacts.getJSONObject(i);
            String name = "";
            String content = "";
            String url = "";
            try {
                name = new String(c.getString(NAME).getBytes("ISO-8859-1"),
                        "UTF-8");
                content = new String(c.getString(CONTENT).getBytes(
                        "ISO-8859-1"), "UTF-8");
                url = new String(c.getString(URL).getBytes("ISO-8859-1"),
                        "UTF-8");
            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
            }

            String decodedName = Html.fromHtml(name).toString();
            String decodedContent = Html.fromHtml(content).toString();
            listproduct.add(new Product(decodedName, decodedContent, url));

            populateListView();
        }

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

}

populateListView()

private void populateListView() {
    ArrayAdapter<Product> adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.listItemView);

    list.setAdapter(adapter);
}

更新
 何时启动我的应用程序“onStatupBool == true”是真的。然后它调用GetJsonAsync。但之后UI不会更新。因此“lastInScreen == totalItemCount”为true,并在更新URL后继续调用GetJsonAsync。
我不知道为什么它在第一次调用后没有更新UI。

0 个答案:

没有答案