首页 > 解决方案 > 我想等待请求响应,使用 Progressbar Android - JAVA 阻止用户屏幕

问题描述

我是 Android (Java) 中的第一个,我想通过在结束时用进度条阻止用户屏幕来获得请求响应。

Intent intent= new Intent(iniciarSesion.this, MainActivity.class);
            intent.putExtra("id",id);
            intent.putExtra("nom_usu",nom_usu);
            intent.putExtra("equipo",equipo);
            intent.putExtra("password",password);
            startActivity(intent);





   if (equipo.equals("S")) {
  añadirequipo x =  new añadirequipo();
  x.nombreCancha(id,getApplicationContext());  }



public void nombreCancha(String idUsuario,Context mContext) {


String URL="url"+idUsuario+"";
    JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(URL, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            JSONObject jsonObject = null;


                for (int i = 0; i < response.length(); i++) {
                    try {
                        jsonObject = response.getJSONObject(i);

                        id_equipo = jsonObject.getString("idequipo");
                        id_usu = jsonObject.getString("idusuario");
                        nombre_equipo = jsonObject.getString("nombre_equipo");
                        cancha = jsonObject.getString("cancha");

                    } catch (JSONException e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.v("Fallo_Json:", e.getMessage());
                    }
                }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error.toString().equals("com.android.volley.ParseError: org.json.JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray")){

                Toast.makeText(mContext, "No existe ese usuario", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "Fallo json"+error.getMessage(), Toast.LENGTH_SHORT).show();
                Log.v ("Fallo_Listener:", error.getMessage());
            }


        }
    }
    );

    requestQueue= Volley.newRequestQueue(mContext);
    requestQueue.add(jsonArrayRequest);

}

nombreCancha 方法设置一个字符串字段变量,然后在片段中检索该变量

public String esperarNombre() {
return cancha;

}

发生的情况是,在响应片段完成之前,我希望应用程序在响应结束时停止,并在响应结束时放置一个 Progressbar。

我知道请求异步线程并且我不想同步我希望应用程序等待它以加载栏结束。

注1:

你好

Toast.makeText(getApplicationContext(), "Bienvenido "+nom_usu+" ", Toast.LENGTH_SHORT).show();
                Intent intent= new Intent(iniciarSesion.this, MainActivity.class);
                intent.putExtra("id",id);
                intent.putExtra("nom_usu",nom_usu);
                intent.putExtra("equipo",equipo);
                intent.putExtra("password",password);
                startActivity(intent);
                if (equipo.equals("S")) {
                    loader.setVisibility(VISIBLE);
                    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
                    añadirequipo x =  new añadirequipo();
                    x.nombreCancha(id,getApplicationContext(),loader);

                }



public void onResponse(JSONArray response) {
                JSONObject jsonObject = null;


                    for (int i = 0; i < response.length(); i++) {
                        try {
                            jsonObject = response.getJSONObject(i);

                            id_equipo = jsonObject.getString("idequipo");
                            id_usu = jsonObject.getString("idusuario");
                            nombre_equipo = jsonObject.getString("nombre_equipo");
                            cancha = jsonObject.getString("cancha");

                        } catch (JSONException e) {
                            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                            Log.v("Fallo_Json:", e.getMessage());
                        }
                    }

               // progressDialog.dismiss();
                loader.setVisibility(GONE);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            }

给我一个错误:尝试在空对象引用上调用虚拟方法 'void android.view.Window.clearFlags(int)'

标签: javaandroidandroid-studio

解决方案


在您的 xml 中使用进度条,因为 ProgressDailog 现在已被删除

 <ProgressBar
        android:id="@+id/loader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminateTintMode="src_atop"
        android:indeterminateTint="@android:color/black"
        android:layout_gravity="center" />

然后在对窗口进行网络调用时设置标志,这将阻止用户交互

loader.visibility = VISIBLE
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                           WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

然后在网络调用完成后清除标志

loader.visibility = GONE
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

推荐阅读