如何在android中加载进度条,直到响应来自服务器

时间:2016-01-29 11:38:18

标签: android progress-bar

嗨在我的应用程序中我向服务器发送一个请求以验证用户,在发送请求后我将该值存储在数据库中并将状态设置为1,一段时间后我将状态更改为2在database.Now我的Android应用程序应该等到状态变为2.为此我在移动进度栏中显示用户。但我的问题是我发送请求进度条停止显示在移动设备中。

这是我尝试过的。

progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.show();
            progressDialog.setTitle("Please Wait");
            progressDialog.setMax(100);
            e1 = edittext.getText().toString();
            //Toast.makeText(MainActivity.this, "" + e1, Toast.LENGTH_SHORT).show();

            AsyncHttpClient client = new AsyncHttpClient();
            final RequestParams params = new RequestParams();
            params.put("sendingJSON", composeJSON());
            client.post("http://192.168.43.137/gpstracker/check_user.php", params, new AsyncHttpResponseHandler() {
                public void onSuccess(String response) {

                    Gson gson = new GsonBuilder().create();
                    try {
                        progressDialog.dismiss();
                        JSONArray arr = new JSONArray(response);
                        for (int i = 0; i < arr.length(); i++) {
                            JSONObject obj = (JSONObject) arr.get(i);
                            String general = obj.get("success").toString();
                            Toast.makeText(getApplicationContext(), ""+general, Toast.LENGTH_LONG).show();

                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                public void onFailure(int statusCode, Throwable error, String content) {
                    if (statusCode == 404) {
                        Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    } else if (statusCode == 500) {
                        Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",Toast.LENGTH_LONG).show();
                    }
                }

            });
        }

    });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()

                {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // what ever you want to do with No option.
                    }
                }

        );

        alert.show();
    }

2 个答案:

答案 0 :(得分:0)

在这种情况下,一个好的解决方案是处理事件。使用事件,您可以通知任何地方正在发生的事情。在您的情况下,您可以在&#34;数据库中状态更改为2时发送事件&#34;并关闭进度对话框。有一个功能强大且易于使用的库来处理名为eventbus的事件:

https://github.com/greenrobot/EventBus

我希望这有助于澄清!

答案 1 :(得分:0)

我建议你使用AsynkTask来做到这一点。您可以在 onPreExecute()方法中创建进度对话框,在 doInBackground()中执行您要执行的操作(发布操作或数据库更新)你想要的,你可以在 onPostExecute()中关闭对话框,并在需要时将结果/数据发送到你的活动中。

public class yourAsyncTask extends AsynkTask<Params, Params, Params>
@Override
protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Charging...");
    pDialog.setCancelable(true);
    pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

        @Override
        public void onCancel(DialogInterface dialog) {
            errorMesaje = "Process cancelled";
            cancel(true);
        }
    });
    pDialog.show();
}

@Override
protected Void doInBackground(Params... params) {
//Do your things here...

@Override
protected void onPostExecute(Void void) {
    super.onPostExecute(void);
    //Use an interface to call your activity and pass the data, you will have to change the attribute of the method in order to do it.
// Dismiss your dialog when your requests have finished
    pDialog.dismiss();
}

我不明白你想要做什么,如果你只是想确保在进度条关闭之前完成后期操作,这个解决方案就没问题了。您也可以在doInBackground()中调用数据库并进行更新。

希望它足够清楚!