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

标签 android mysql android-volley

我对 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 = 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();

        }

关于android - 在Android应用程序中使用PHP和MYSQL创建Volley和RequestQueue错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667538/

相关文章:

Android 模拟器 vs 手机 opengl 不一致

mysql - 在 MySQL 的文本列中搜索字符串

安卓。 Volley 。 volleyError.networkResponse 为空

android - 微调器不显示所选值

android - LinkedIn 无法在 WebView 中注销

PHP - 过滤输入的编码问题

mysql - 非法组函数 SQL

android - 使用 Volley 处理 JSONRequest 中的空响应

java - 如何比较从Web服务中提取的3个变量(equalsIgnoreCase)?

android - 直接实现监听器或在类上实现(性能)