gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:51:26 25 4
gpt4 key购买 nike

我对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com