在Android应用程序中使用PHP和MYSQL创建Volley和RequestQueue错误

在Android应用程序中使用PHP和MYSQL创建Volley和RequestQueue错误,android,mysql,android-volley,Android,Mysql,Android Volley,我对Android编程和尝试非常陌生。单击一个按钮后,我需要运行该方法,如果没有响应,则希望向类似用户的服务器显示一条无法访问的消息。我写了一些代码。但即使我关闭了mysql服务器,它也没有运行错误。我如何使其给出错误: 下面是示例代码 public void addPollDB() { RequestQueue requestQueue; String urlSend = "http://192.168.1.221/poll/insertPoll.php"; isPu

我对Android编程和尝试非常陌生。单击一个按钮后,我需要运行该方法,如果没有响应,则希望向类似用户的服务器显示一条无法访问的消息。我写了一些代码。但即使我关闭了mysql服务器,它也没有运行错误。我如何使其给出错误: 下面是示例代码

public void addPollDB() {
    RequestQueue requestQueue;
    String urlSend = "http://192.168.1.221/poll/insertPoll.php";

    isPublicSW = (((Switch)findViewById(R.id.isPublicSW)).isChecked());

    if (!isPublicSW) ispublic = 0;
    else {
        ispublic = 1;
    }

    if(!TextUtils.isEmpty(q.getText().toString())){
        requestQueue = Volley.newRequestQueue(this);

        StringRequest request = new StringRequest(Request.Method.POST, urlSend, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            VolleyError volleyError;
            @Override
            public void onErrorResponse(VolleyError error) {
                String message = null;
                if (volleyError instanceof NetworkError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                } else if (volleyError instanceof ServerError) {
                    message = "The server could not be found. Please try again after some time!!";
                } else if (volleyError instanceof AuthFailureError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                } else if (volleyError instanceof ParseError) {
                    message = "Parsing error! Please try again after some time!!";
                } else if (volleyError instanceof NoConnectionError) {
                    message = "Cannot connect to Internet...Please check your connection!";
                } else if (volleyError instanceof TimeoutError) {
                    message = "Connection TimeOut! Please check your internet connection.";
                }
                Context context = getApplicationContext();
                CharSequence polladded = "Poll can not be added.";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context, polladded, duration);
                toast.show();

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> parameters = new HashMap<String, String>();
                parameters.put("user_id",user_id.toString());
                parameters.put("question",q.getText().toString());
                parameters.put("ch1",choice1.getText().toString());
                parameters.put("ch2",choice2.getText().toString());
                parameters.put("ch3",choice3.getText().toString());
                parameters.put("ch4",choice4.getText().toString());
                parameters.put("ch5",choice5.getText().toString());
                parameters.put("isPublic",ispublic.toString());

                return parameters;
                //return null;

            }

        };

        requestQueue.add(request);

        Intent returnIntent = new Intent();
        setResult(RESULT_OK, returnIntent);

        finish();


    } else {

        Context context = getApplicationContext();
        CharSequence noinfo = "You need to enter some information!";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, noinfo, duration);
        toast.show();

    }
}
您没有在祝酒词上显示消息

使用:

而不是:

试试这个:


很高兴知道这一点。如果我的回答似乎有用,请放弃投票。提前感谢:
 Toast toast = Toast.makeText(context, message, duration); 
 toast.show();
 Toast toast = Toast.makeText(context, polladded, duration);
 toast.show();
    public void onErrorResponse(VolleyError error) {
            String message = null;
            if (volleyError instanceof NetworkError) {
                message = "Cannot connect to Internet...Please check your connection!";
            } else if (volleyError instanceof ServerError) {
                message = "The server could not be found. Please try again after some time!!";
            } else if (volleyError instanceof AuthFailureError) {
                message = "Cannot connect to Internet...Please check your connection!";
            } else if (volleyError instanceof ParseError) {
                message = "Parsing error! Please try again after some time!!";
            } else if (volleyError instanceof NoConnectionError) {
                message = "Cannot connect to Internet...Please check your connection!";
            } else if (volleyError instanceof TimeoutError) {
                message = "Connection TimeOut! Please check your internet connection.";
            }
            Context context = getApplicationContext();
            CharSequence polladded = "Poll can not be added.";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, message, duration);
            toast.show();

        }