【问题标题】:About task onPostExecute关于任务 onPostExecute
【发布时间】:2016-06-08 19:51:09
【问题描述】:

我有一个运行良好的类 AsyncTask,但是在这个类中,我在 onPreExecute 中有一个 ProgressDialog,它运行正常,但是我需要在完成该过程时关闭 processdialog,我将代码放在 onPostExecute 中,这个方式:

protected void onPostExecute() {
    progress.dismiss();
}

但它什么也没做。对话框不会关闭。 这是全部代码:

public class JsonRequest extends AsyncTask<String, Void, ArrayList<String>> {

private final Context context;
private ArrayList<String> stringArray = new ArrayList<String>();
private ProgressDialog progress;

public JsonRequest(Context c){
    this.context = c;
}

protected void onPreExecute(){
    progress= new ProgressDialog(this.context);
    progress.setMessage("Loading");
    progress.show();
}

@Override
protected ArrayList<String> doInBackground(String... params) {
    try {

        final String POST_PARAMS = params[1];

        URL obj = new URL(params[0]);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");

        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();
        System.out.println("POST Response Code :: " + responseCode);

        if (responseCode == HttpURLConnection.HTTP_OK) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            JSONArray myListsAll= new JSONArray(response.toString());
            for(int i=0;i<myListsAll.length();i++){

                JSONObject jsonObject = myListsAll.getJSONObject(i);
                this.stringArray.add(jsonObject.toString());
            }

        } else {
            System.out.println("POST request not worked");
        }


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return this.stringArray;
}

protected void onPostExecute() {
    progress.dismiss();
}

所以我打电话:

public class MyActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private void createListView() {
        itens = new ArrayList<ItemFeedsListView>();

        String[] itensUrl = {"url","data"};
        JsonRequest task = new JsonRequest(this);
        try {
            ArrayList<String> result = task.execute(itensUrl).get();

            for(int i = 0 ; i < result.size() ; i++) {
                String currentList = result.get(i);
                JSONObject jsonobject = new JSONObject(currentList);

                ItemFeedsListView item = new ItemFeedsListView(jsonobject.optString("value1"),jsonobject.optString("value2"),jsonobject.optString("valeu3"),jsonobject.optString("value4"),jsonobject.optString("value5"), R.mipmap.eu, R.mipmap.logo);
                itens.add(item);

            }

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

        //Cria o adapter
        adapterListView = new AdapterFeedsListView(this, itens);

        //Define o Adapter
        listView.setAdapter(adapterListView);
        //Cor quando a lista é selecionada para ralagem.
        listView.setCacheColorHint(Color.TRANSPARENT);
    }
}

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    你没有调用正确的 onPostExecute:

    你需要:

    @Override
     protected void onPostExecute(ArrayList<String> data) {
        super.onPostExecute(data);
        progress.dismiss();
    }
    

    问题是您没有通过添加onPostExecute() 来覆盖 AsyncTask 类中的方法。您需要添加 @Override 注释并传入 AsyncTask 类的返回类型。

    【讨论】:

      【解决方案2】:

      您没有正确使用 AsyncTask。看这行代码:

      ArrayList<String> result = task.execute(itensUrl).get();
      

      这是在启动一个 AsyncTask(它通常会在后台线程中完成大部分工作),然后立即阻塞主线程以使用 get() 接收其结果方法。在这种情况下,您甚至可以不使用 AsyncTask,因为 AsyncTask 的目的是让主线程不必执行冗长的操作。

      相反,您应该单独在 onPostExecute() 中处理后台线程完成的工作。

      【讨论】:

      • 你好 Doug,你是对的,使用这种方式 uithread 被阻止,如何创建这个主题来修复 inner_class7 显示解决的进程加载错误,我按照你的建议更改了我的代码并创建了其他话题来谈论。我希望你把你的意见放在那里。谢谢谢谢这是链接:stackoverflow.com/questions/35685688/suggestion-about-asynctask
      【解决方案3】:

      您也可以通过这种方式设置可见性:

      @Override
       protected void onPostExecute(ArrayList<String> data) {
          super.onPostExecute(data);
          progress.setVisibility(View.GONE);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-10
        • 1970-01-01
        • 2020-03-12
        • 2023-03-19
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        相关资源
        最近更新 更多