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

时间:2017-04-27 20:40:33

标签: android mysql android-volley

我非常非常熟悉Android编程和尝试。单击按钮后,我需要运行方法,如果没有响应,则要向用户显示“server unreachable”消息。我写了一些代码。但即使我关闭了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();

    }
}

1 个答案:

答案 0 :(得分:1)

您未在messages上显示Toast

使用:

 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();

        }