【问题标题】:How to load progress bar in android until response comes from server如何在android中加载进度条,直到响应来自服务器
【发布时间】:2016-05-07 03:12:17
【问题描述】:

嗨,在我的应用程序中,我正在向服务器发送一个请求以验证用户,在发送请求后,我将该值存储在数据库中并将状态设为 1,一段时间后我将状态更改为 2在数据库中。现在我的 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();
    }

【问题讨论】:

    标签: android progress-bar


    【解决方案1】:

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

    https://github.com/greenrobot/EventBus

    我希望这有助于澄清!

    【讨论】:

      【解决方案2】:

      我会推荐你​​使用 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() 到你的数据库并更新它。

      希望它足够清楚!

      【讨论】:

      • 简而言之,我将用户 ID 发送到数据库并将状态设置为 1,管理员可以将我的状态更改为 2,因此当状态更改为 2 时,进度条应该消失。
      • 我不知道管理员需要多长时间才能更改您的状态,但如果您能够在 doInBackground() 中处理它应该没问题。我的意思是,您可以检查状态,直到获得您的指定值 2(如果需要更多时间,请使用 while 循环或延迟的新线程),在 doInBackgroung 方法中执行所有这些操作,然后您可以关闭状态为 2 时的对话框。
      • 你做到了吗?
      • 请帮我解决这个问题 [link]stackoverflow.com/questions/35182540/…
      猜你喜欢
      • 1970-01-01
      • 2016-12-10
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      相关资源
      最近更新 更多