我是android工作室的初学者,我在创建新帐户注册码时遇到了问题。按下注册按钮显示(无连接错误)为吐司信息。这是由于我(如果条件)发生错误。那么这个错误的解决方案是什么(没有连接错误)或者是什么原因?
如果条件代码
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError) {
Toast.makeText(getApplicationContext(), "Timeout Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof NoConnectionError) {
Toast.makeText(getApplicationContext(), "No connection Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
Toast.makeText(getApplicationContext(), "AuthFailure Error!", Toast.LENGTH_SHORT).show();
} else if (error instanceof NetworkError) {
Toast.makeText(getApplicationContext(), "Network Error !", Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
Toast.makeText(getApplicationContext(), "Server Error !", Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
Toast.makeText(getApplicationContext(), "JSON Parse Error !", Toast.LENGTH_SHORT).show();
}
}
}
在进行API调用之前检查互联网连接,您可以使用以下功能检查互联网连接是否已连接。
public boolean isConnectedToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null)
if (info.getState() == NetworkInfo.State.CONNECTED || info.getState() == NetworkInfo.State.CONNECTING) {
if (info.isAvailable()) {
return true;
}
}
}
return false;
}
更多提示: