java - 很多时候在android中显示没有互联网连接
问题描述
我正在使用JsonArrayRequest
将 json 数据检索到我的 android 应用程序中。一切正常,但很多时候它显示network error
我已经设置了TimeOut...! No Internet
Toast。但我有互联网连接,甚至显示网络错误吐司。我正在使用 OnErrorResponse:
private void jsonrequest() {
request = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
loading.setVisibility(View.GONE);
JSONObject jsonObject = null ;
for (int i = 0 ; i < response.length(); i++ ) {
try {
jsonObject = response.getJSONObject(i) ;
Anime anime = new Anime() ;
anime.setName(jsonObject.getString("title"));
anime.setDescription(jsonObject.getString("description"));
anime.setRating(jsonObject.getString("date"));
anime.setCategorie(jsonObject.getString("category"));
anime.setStudio(jsonObject.getString("admin"));
anime.setImage_url(jsonObject.getString("thumbnail"));
lstAnime.add(anime);
} catch (JSONException e) {
e.printStackTrace();
}
}
setuprecyclerview(lstAnime);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
//This indicates that the request has either time out or there is no connection
Toast.makeText(MainActivity.this, "TimeOut...! No Internet",
Toast.LENGTH_LONG).show();
} else if (error instanceof AuthFailureError) {
// Error indicating that there was an Authentication Failure while performing the request
Toast.makeText(MainActivity.this, "Failed To Receive Data",
Toast.LENGTH_LONG).show();
} else if (error instanceof ServerError) {
//Indicates that the server responded with a error response
Toast.makeText(MainActivity.this, "Our Server Under Maintenance",
Toast.LENGTH_LONG).show();
} else if (error instanceof NetworkError) {
//Indicates that there was network error while performing the request
Toast.makeText(MainActivity.this, "Network Not Responding",
Toast.LENGTH_LONG).show();
} else if (error instanceof ParseError) {
// Indicates that the server response could not be parsed
Toast.makeText(MainActivity.this, "Please Reload Again!",
Toast.LENGTH_LONG).show();
}
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(request) ;
}
我不明白问题出在哪里?你能帮我解决这个问题吗?
谢谢。
解决方案
推荐阅读
- python - Pandas 使用 groupby 创建新列并避免循环
- sql - 用于在 csv 文件中获取 woocommerce 产品详细信息的 SQL 查询
- flutter - Flutter在gridview中显示额外的空间我该如何删除它
- python - Plotly:在折线图中的最后一个值处注释标记
- java - 数组 - 以分钟为单位的时差
- sql - SQL 使用计数和求和
- javascript - Bootstrap 模式中的画布给了我错误的鼠标坐标
- c++ - 如果 IOPCServer 没有发布怎么办?
- c# - 在 Unity 中使用矢量数学生成游戏对象?
- zoho - 生成授权令牌 ZOHO CRM