0

我正在为我的应用程序使用 Web 服务。方法 callWebService() 中的数据正在增加。我在填充我的 ListView 时显示 ProgressDialog。我还想检查是否没有网络连接来关闭 ProgressDialog 并显示 AlertDialog用户。有人可以帮我吗这是我的代码

     myProgressDialog = ProgressDialog.show(ExchangeMoneMK1Activity.this,
                     "Please wait...", "Loading...", true);
         Thread thread = new Thread(this);
         thread.start();


 private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}




private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
        case 0:

            myProgressDialog.dismiss();
            int resID = R.layout.list_item;
            aa = new ExchangeRateAdapter(ExchangeMoneMK1Activity.this, resID,currencyList);

            lw.setAdapter(aa);
            break;
        }


    }

};

public void run() {
    // TODO Auto-generated method stub

    try {
        if(isNetworkAvailable())
        callWebService();
        handler.sendEmptyMessage(0);
    }


     catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

我还没有警报对话框,因为如果没有互联网连接,进度条不会消失,我已经放入了 AndroidManifest 权限

有人可以帮我吗?

4

2 回答 2

0

finally 也与 try catch 和 put handler.sendEmptyMessage(0) 一起使用;在最后...

因为我认为你得到了任何 excption 并且你的流程跳转到 callWebService() ..... 而没有到达 handler.sendEmptyMessage(0); 线........

于 2012-06-02T17:11:46.710 回答
0

使用异步任务。它会简单得多。一般模式如下。可能会有一些小的编译错误,但是,这会给你一个想法。

class MyTask extends AsyncTask<Void, Void, ArrayList> {
    protected Void doInBackground(Void... params) {
        ArrayList currencyList;
        if(isNetworkAvailable())
            currencyList = callWebService();
        return currencyList;
    }
    void onPreExecute() {
        myProgressDialog = ProgressDialog.show(ExchangeMoneMK1Activity.this,
                 "Please wait...", "Loading...", true);
    }
    void onPostExecute(ArrayList currencyList) {
        myProgressDialog.dismiss();
        if (currencyList == null) {
            //Handle error
            return;
        }
        int resID = R.layout.list_item;
        aa = new ExchangeRateAdapter(ExchangeMoneMK1Activity.this, resID,currencyList);

        lw.setAdapter(aa);
    }
}
于 2012-06-02T17:14:47.593 回答