在AsyncTask中获取数据

时间:2014-05-23 09:09:27

标签: android azure android-asynctask

我正在尝试使用此方法从Microsoft Azure中提取数据。问题是它有时可能非常慢,我需要在共享首选项中使用这些数据来执行应用程序中的任何其他操作。如何创建一个等待获取数据的加载对话框?我尝试将此方法放在AsyncTask doInBackground()方法中,但对话框只会出现,然后在一毫秒后消失。这样做的正确方法是什么?我正在阅读有关stackoverflow的类似主题,但从未找到解决方案。

谢谢!

private class LoadViewTask extends AsyncTask<String, Void, Boolean>  
    {  
        private ProgressDialog dialog;
        private MainActivity activity;

        public LoadViewTask(MainActivity activity) {

            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }
        private Context context;
        @Override  
        protected void onPreExecute()  
        {  
            //Create a new progress dialog  
            dialog = ProgressDialog.show(MainActivity.this,"Loading...",  
                    "", false, false);  
        }  

        //The code to be executed in a background thread.  
        @Override  
        protected Boolean doInBackground(final String... args)  
        {  

            try  
            {  

                    mClient.invokeApi("getsettings", jObj, new ApiJsonOperationCallback() {

                        @Override
                        public void onCompleted(JsonElement result, Exception error,
                                ServiceFilterResponse response) {
                            SharedPreferences settings = getSharedPreferences("SettingsPrefs", 0);
                            SharedPreferences.Editor editor = settings.edit();
                            if (error != null) {
                                System.out.println("Error");
                            } else {
                                JsonObject res = result.getAsJsonObject();
                                try {


                                    if(res.get("gender").toString().equals("null")){
                                        userGender = res.get("gender").toString();
                                        editor.putString("gender", userGender);
                                    } else {
                                        int index1 = res.get("gender").toString().indexOf("\"");
                                        int index2 = res.get("gender").toString().lastIndexOf("\"");
                                        editor.putString("gender", res.get("gender").toString().substring(index1+1, index2));

                                    }
                                    if(res.get("dob").toString().equals("null")){
                                        userDob = res.get("dob").toString();
                                        editor.putString("dob", userDob);
                                    } else {
                                        editor.putString("dob", res.get("dob").toString().substring(1, 11));

                                    }
                                    if (res.get("club").isJsonNull()) {
                                        userClub = 0;
                                        editor.putInt("userClub", userClub);
                                        System.out.println("userclub is null in MA: "+userClub);
                                    } else {
                                        editor.putInt("userClub", res.get("club").getAsInt());


                                    }

                                    editor.commit();
                                } catch (Exception e) {
                                    Log.e("Error: ", e.toString());
                                }
                            }

                        }
                    });

            }  
            catch (Exception e)  
            {  
                e.printStackTrace();  
            }  
            return null;
        }  

        @Override  
        protected void onPostExecute(final Boolean success)  
        {  
            //close the progress dialog  
            dialog.dismiss();
        }  
    }   

3 个答案:

答案 0 :(得分:0)

创建ProgressDialogshow()之前,在完成所有必需流程后,在mClient.invokeApi上致电dismiss()onCompleted

答案 1 :(得分:0)

请按照以下步骤操作

1)创建方法说loadDataFromServer()并将代码放在

public void loadDataFromServer() {
        dialog = ProgressDialog.show(MainActivity.this, "Loading...", "", false, false);

        try {

            mClient.invokeApi("getsettings", jObj, new ApiJsonOperationCallback() {

                @Override
                public void onCompleted(JsonElement result, Exception error, ServiceFilterResponse response) {
                    SharedPreferences settings = getSharedPreferences("SettingsPrefs", 0);
                    SharedPreferences.Editor editor = settings.edit();
                    if (error != null) {
                        System.out.println("Error");
                    } else {
                        JsonObject res = result.getAsJsonObject();
                        try {

                            if (res.get("gender").toString().equals("null")) {
                                userGender = res.get("gender").toString();
                                editor.putString("gender", userGender);
                            } else {
                                int index1 = res.get("gender").toString().indexOf("\"");
                                int index2 = res.get("gender").toString().lastIndexOf("\"");
                                editor.putString("gender", res.get("gender").toString().substring(index1 + 1, index2));

                            }
                            if (res.get("dob").toString().equals("null")) {
                                userDob = res.get("dob").toString();
                                editor.putString("dob", userDob);
                            } else {
                                editor.putString("dob", res.get("dob").toString().substring(1, 11));

                            }
                            if (res.get("club").isJsonNull()) {
                                userClub = 0;
                                editor.putInt("userClub", userClub);
                                System.out.println("userclub is null in MA: " + userClub);
                            } else {
                                editor.putInt("userClub", res.get("club").getAsInt());

                            }

                            editor.commit();

                            dialog.dismiss(); // / DISMISS DIALOG HERE
                        } catch (Exception e) {
                            Log.e("Error: ", e.toString());
                        }
                    }

                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

2)将此方法称为

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        loadDataFromServer();

    }

3)关闭onCompleted()方法内的对话框。 (我已添加该行)。只需复制方法并调用它即可。

答案 2 :(得分:0)

尝试将indeterminate设置为true,

//Create a new progress dialog  
            dialog = ProgressDialog.show(MainActivity.this,"Loading...",  
                    "", true, false);