Android-Java添加,请等待消息

时间:2019-01-19 20:07:59

标签: android android-asynctask

我的问题是如何添加消息“正在加载,请稍候”。直到发出请求并将响应解析到表中。

有使用这种方法的方法吗?

像这样,当我运行此程序时,我不知道是否没有响应数据或尚未收到响应。

StringRequest postRequest = new StringRequest(Request.Method.POST, apiURL,
        new Response.Listener<String>()
        {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObj = new JSONObject(response);

                    JSONArray jsonArray = jsonObj.getJSONArray("sales");

                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject employee = jsonArray.getJSONObject(i);

                        TableRow tbrow = new TableRow(  MainActivity.this   );
                        TextView t1v = new TextView(MainActivity.this);

                        t1v.setText(employee.getString("saletype"));
                        t1v.setTextColor(Color.BLACK);
                        stk.addView(tbrow);

                        // .......

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }
) {
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String>  params = new HashMap<String, String>();
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        params.put("store", spinner.getSelectedItem().toString());
        return params;
    }
};

mQueue.add(postRequest);

1 个答案:

答案 0 :(得分:1)

在队列中添加请求对象时显示进度栏。

mQueue.add(postrequest); 

progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading ...Please Wait"); 
progressDialog.show(); 

然后在收到响应时将其关闭。

try {
     JSONObject jsonObj = new JSONObject(response);

     JSONArray jsonArray = jsonObj.getJSONArray("sales");

     for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject employee = jsonArray.getJSONObject(i);

      TableRow tbrow = new TableRow(MainActivity.this);
      TextView t1v = new TextView(MainActivity.this);

      t1v.setText(employee.getString("saletype"));
      t1v.setTextColor(Color.BLACK);
      stk.addView(tbrow);

      // ....... 

     }

    progressDialog.dismiss();
    } catch (Exception e) {
     progressDialog.dismiss();
    }

 @Override 
        public void onErrorResponse(VolleyError error) { 
          progressDialog.dismiss();
        }