Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:AsyncTask从url获取Json_Android_Json_Android Asynctask - Fatal编程技术网

Android:AsyncTask从url获取Json

Android:AsyncTask从url获取Json,android,json,android-asynctask,Android,Json,Android Asynctask,请帮我: 当使用Android 2.2时,这段代码工作得非常好:(在标记为注释的行中直接调用getJSONFromUrl) 但是在3.2中。不允许直接调用getJSONFromUrl。所以我使用Asyntask: 正如我在上面的代码块中使用的。那么它就不能正常工作了 private class GetJsonAsync extends AsyncTask<String, Void, JSONObject> { @Override protected JSONObje

请帮我:
当使用Android 2.2时,这段代码工作得非常好:(在标记为注释的行中直接调用getJSONFromUrl)

但是在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) {

    }

}
私有类GetJsonAsync扩展异步任务{
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
JSONParser jParser=新的JSONParser();
JSONObject json=jParser.getJSONFromUrl(currentURL);
RetreiveListStory(json);
返回null;
}
@凌驾
受保护的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();
    }

}
private void RetreiveListStory(JSONObject json){
试一试{
//获取联系人数组
contacts=json.getJSONArray(响应);
如果(contacts.length()==0){
onStopLoadMore=true;
}
//通过所有触点循环
对于(int i=0;i
populateListView()

private void populateListView(){
ArrayAdapter=新的MyListAdapter();
ListView列表=(ListView)findViewById(R.id.listItemView);
list.setAdapter(适配器);
}
更新:
当启动我的应用程序时。“onStatupBool==true”是true。然后调用GetJsonAsync。但在此之后,用户界面不会更新。因此“lastInScreen==totalItemCount”为true,并在更新URL后继续调用GetJsonAsync。

我不知道为什么它在第一次调用后不更新UI

你说它不能正常工作是什么意思?看看代码,他可能是说它根本不能工作,因为他从来没有正确地实现过
onPostExecute
。到@VanDang——一旦得到结果,您需要在
onPostExecute
中处理所有UI更新。一次只允许运行一个Asynctask,但您尝试使用if-else梯形图一次启动多个Asynctask。因此,在上一次完成时,使用onPostExecute启动下一个异步任务。@323go我更新了上面的问题。然后启动我的应用程序。“onStatupBool==true”是true。然后调用GetJsonAsync。但在此之后,用户界面不会更新。因此“lastInScreen==totalItemCount”为true,并继续调用GetJsonAsync。我不知道为什么在第一次呼叫后它不更新UI。有人知道如何修复它吗?在这种情况下我真的需要帮助。请
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();
    }

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

    list.setAdapter(adapter);
}