0

我有这个问题,当我尝试加载新视图时,在我得到服务响应之前我得到黑屏。现在为了解决这个问题,我使用了 AsyncTask 并且我已经解决了这个问题,但问题是我没有从我的服务中获得输出。下面是我的代码。

class GetInfo extends AsyncTask<String, String, String> {
    /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(NewHomeView.this);
                pDialog.setMessage("Loading...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            /**
             * Creating player
             * */
            protected String doInBackground(String... args) {



                        goal = MMBusinessFacade.baseURL;
                        goal = goal + "getGoal.jsp?acno=" + acnt;
                        goal = goal.replaceAll(" ", "%20");
                        res = MMBusinessFacade.getXML(goal, NewHomeView.this);
                        NewHomeView.res1 = gp.getOutput(res);
                        System.out.println("URL:" + goal);
                        System.out.println("Output:" + res1);


                        /********************************************************************************************/
                        spnrshi1 = MMBusinessFacade.baseURL;
                        spnrshi1 = spnrshi1 + "getFunds.jsp?acno=" + acnt;
                        spnrshi1 = spnrshi1.replaceAll(" ", "%20");
                        res = MMBusinessFacade.getXML(spnrshi1, NewHomeView.this);
                        System.out.println("URL" + spnrshi1);
                        list1 = fdp.getOutputOrg1(res);
                        list = fdp.getOutputOrg(res);

                        if (list1 != null && list1.size() > 0) {
                            for (int j = 0; j < list1.size(); j++) {
                                FundsAvailable prpobj = (FundsAvailable) list1.get(j);
                                FundDetail prpobj1 = (FundDetail) list.get(j);
                                ite = prpobj.getAmount() + "  " + prpobj.getCurrency();
                                System.out.println("Item:" + ite);
                                NewHomeView.favail = prpobj1.getAmount() + " " + prpobj1.getCurrency();
                                NewHomeView.cur = prpobj.getCurrency();
                            }
                        }



                return null;
      }
            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) 
            {
                // dismiss the dialog once done
                pDialog.dismiss();
                gl.setText(NewHomeView.res1);
                avilfund.setText(NewHomeView.ite);
                fndraised.setText(NewHomeView.favail);
            }

        }

请给您宝贵的建议。谢谢

4

2 回答 2

0

我认为:U 不应该在 doInBackground 函数中使用 runOnUiThread。否则,doInBackground 将立即返回。并且您将使用 onPostExecute(null) 获得 null。所以,失败了。正确的代码:

protected String doInBackground(String... args) {

                        goal = MMBusinessFacade.baseURL;
                        goal = goal + "getGoal.jsp?acno=" + acnt;
                        goal = goal.replaceAll(" ", "%20");
                        res = MMBusinessFacade.getXML(goal, NewHomeView.this);
                        NewHomeView.res1 = gp.getOutput(res);
                        System.out.println("URL:" + goal);
                        System.out.println("Output:" + res1);


                        /********************************************************************************************/
                        spnrshi1 = MMBusinessFacade.baseURL;
                        spnrshi1 = spnrshi1 + "getFunds.jsp?acno=" + acnt;
                        spnrshi1 = spnrshi1.replaceAll(" ", "%20");
                        res = MMBusinessFacade.getXML(spnrshi1, NewHomeView.this);
                        System.out.println("URL" + spnrshi1);
                        list1 = fdp.getOutputOrg1(res);
                        list = fdp.getOutputOrg(res);

                        if (list1 != null && list1.size() > 0) {
                            for (int j = 0; j < list1.size(); j++) {
                                FundsAvailable prpobj = (FundsAvailable) list1.get(j);
                                FundDetail prpobj1 = (FundDetail) list.get(j);
                                ite = prpobj.getAmount() + "  " + prpobj.getCurrency();
                                System.out.println("Item:" + ite);
                                NewHomeView.favail = prpobj1.getAmount() + " " + prpobj1.getCurrency();
                                NewHomeView.cur = prpobj.getCurrency();
                            }
                        }

           }
      }
于 2012-07-27T09:02:25.373 回答
0

你不应该runOnUiThread在你的doInBackground中使用它,它会让你失去使用 AsyncTask 的兴趣。在 doInBackground 中与 UI 通信的最佳方式是使用进度更新过程:

只需在您的 asynTask 中实现:

@Override
protected void onProgressUpdate(MyProgressObject... mpo) {
    super.onProgressUpdate(mpo);
    //my UI update here
}

当您需要从内部更新 UI 时,doInBackground只需调用publishProgress(MyProgressObject mpo)

于 2012-07-27T09:02:51.550 回答