网络通话过程中显示ProgressDialog,直到它完成 [英] Show ProgressDialog during a network call until it's FINISHED

查看:153
本文介绍了网络通话过程中显示ProgressDialog,直到它完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的非常沮丧,因为我一直在试图实现一个超级简单的负重轮,而在网络上呼叫等待。我已经搜索并阅读几十个SO问题,我只是觉得我必须缺少的东西,除非没有人真正做了什么,我试图做的。我曾尝试下去了的AsyncTask 路线,但是这不是我想要的。

I am very frustrated as I've been trying to implement a super simple loading wheel while waiting on a network call. I have searched and read dozens of SO questions and I just feel like I must be missing something, unless nobody really does what I'm trying to do. I have tried going down the AsyncTask route, but that's not what I want.

我还要说,现在我的应用程序完美的作品,它只是根据画面的转变似乎挂起,因为它等待在网络上。我只想要一个负重轮,这样在1-2秒内用户知道应用程序正在运行,且没有冻结。

Let me also say that right now my app works perfectly, it's just that the transition from screen to screen appears to hang as it waits on the network. I just want a loading wheel so that in the 1-2 seconds the user knows the app is working and didn't freeze.

下面就是我目前的网络电话如下:

Here's what my current network call looks like:

 private static String sendDataToServer(String arg1, String arg2)
 {        
    Thread dbThread = new Thread()
    {
        public void run()
        {
             // do the call that takes a long time    
        }
    };
    dbThread.start();
    try {
        // I do this so that my program doesn't continue until
        // the network call is done and I have received the information
        // I need to render my next screen
        dbThread.join();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
 }

现在,为什么我不能只是添加 ProgressDialog 这样吗?如果我这样做,progressDialog永远不会出现。

Now, why can't I just add the ProgressDialog like this? If I do this, the progressDialog never appears.

 private static String sendDataToServer(String arg1, String arg2)
 {
    final ProgressDialog progress = new ProgressDialog(BaseActivity.getInstance());
    progress.setIndeterminate(true);
    progress.setMessage("Loading...");
    progress.show();

    Thread dbThread = new Thread()
    {
        public void run()
        {
             // do the call that takes a long time    
        }
    };
    dbThread.start();
    try {
        dbThread.join();
        progress.dismiss();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
 }

我想我坚持,因为网络电话需要放在从UI线程一个单独的线程,但我不想继续在我的应用程序,因为我需要调用的结果继续进行。但是,如果我做的的Thread.join()我抱了一切。我认为我需要的AsyncTask 但很快就走下坡路。下面是我对这个问题,如果你很好奇。
<一href=\"http://stackoverflow.com/questions/15153919/androids-asynctask-multiple-params-returning-values-waiting\">Android's AsyncTask的:多参数,可以返回值,等待

I think I'm stuck because the network call needs to be on a separate thread from the UI thread, yet I don't want to continue in my application because I need the results of that call to continue. But if I do thread.join() I hold up everything. I thought I needed AsyncTask but that went downhill quickly. Here's my question on that if you're curious. Android's AsyncTask: multiple params, returning values, waiting

如何赫克我只是显示一个对话框装载虽然这调用发生,无需通过我的应用程序的其他部分继续?

How the heck to I just show a loading dialog while this call happens without proceeding through the rest of my application?

修改

下面是我的的AsyncTask 的尝试。

private class PostToFile extends AsyncTask<PostToFile, Void, Void>{
    private String functionName;
    private ArrayList<NameValuePair> postKeyValuePairs;
    private String result = "";

    public PostToFile(String function, ArrayList<NameValuePair> keyValuePairs){
        functionName= function;
        postKeyValuePairs = keyValuePairs;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(BaseActivity.getInstance(), "Loading", "Please wait...", true, false);
    }

    @Override
    protected Void doInBackground(PostToFile... params) {                       
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair(FUNCTION_KEYWORD, functionName));
        for (int i = 0; i < postKeyValuePairs.size(); i++)            {
            nameValuePairs.add(postKeyValuePairs.get(i));
        }

        try{
            // ***do the POST magic.***
            result = response.toString();
        }
        catch (Exception e){
             // clean up my mess
        }

        return null;
    }

    private String getResult(){
        return result; // can I use this somehow???
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
    }
 }

当我使用它:

        new PostToPHP(FUNCTION_NAME, postPairings){
            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                try
                {
                    if (result != null && !result.startsWith("null"))
                    {
                        JSONArray jArray = new JSONArray(result);
                        parseData(jArray);
                    }
                }
                catch (JSONException e)
                {
                    Log.e(Constants.LOG_TAG, e.toString());
                }
            };
        }.execute()

问题是,我有一对夫妇这些调用的背靠背,而且他们每个依赖对方。所以,第一个开始,而第二个开始的立即第一个启动后,但之前第一个是的完成即可。所以,我得到错误的行为。我怎样才能启动第二个电话后,才第一次是完全做得到?

The problem is, I have a couple of these calls back to back, and they're each dependent on each other. So the first one starts, and the second one starts immediately after the first one starts, but before the first one is finished. So I get erroneous behavior. How can I start the second call only after the first is completely done?

推荐答案

也许这会的工作,我没有测试过,但你可以尝试:

Maybe this will work, I haven't tested, but you can try:

public class MyTask extends AsyncTask<String, Void, String> {
    private int flag;


    public MyTask(int flag) {
        this.flag = flag;
    }


    @Override
    protected String doInBackground(String... params) {
        switch (flag) {
            case 1:
                return doNetworking1();
            break;
            case 2:
                return doNetworking2();
            break;
            case 3:
                return doNetworking3();
            break;
            default:
                return doNetworking1();

        }
    }

    @Override
    protected void onPreExecute() {
        //show progress dialog
    }

    @Override
    protected void onPostExecute(String s) {
        //hide progress dialog
        switch (flag) {
            case 1: //do something with result
                new MyTask(2).execute();
                break;
            case 2: //do other stuff
                new MyTask(3).execute();
                break;
            case 3: //do event more stuff
                break;
            default:
                //do something
        }
    }
}

和用法:

new MyTask(1).execute();

这篇关于网络通话过程中显示ProgressDialog,直到它完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆