【问题标题】:Create error with Volley and RequestQueue using PHP and MYSQL in Android App在 Android 应用程序中使用 PHP 和 MYSQL 使用 Volley 和 RequestQueue 创建错误
【发布时间】:2017-04-27 20:40:33
【问题描述】:

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

    }
}

【问题讨论】:

    标签: android mysql android-volley


    【解决方案1】:

    您的Toast 上没有显示messages。

    用途:

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

    【讨论】:

    • 很高兴知道这一点。如果我的回答似乎有用,请投赞成票。在此先感谢:)
    猜你喜欢
    • 2013-10-23
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 2020-09-22
    • 2015-04-24
    相关资源
    最近更新 更多