java - Android Volley - 检查互联网状态

标签 java android android-volley

在我使用 Volley 之前,和往常一样,我使用 AsyncTask 检查我的互联网状态。

这是我在 AsyncTask 中所做的:

private class NetCheck extends AsyncTask<String, Void, Boolean> {

    @Override
    protected Boolean doInBackground(String... args) {
        // get Internet status
        return cd.isConnectingToInternet();
    }

    protected void onPostExecute(Boolean th) {
        if (th == true) {
            new LoadCategories().execute();
        } else {
            Toast.makeText(CategoryActivity.this, "Unable to connect to server",
                    Toast.LENGTH_LONG).show();
        }
    }
}

这是isConnectingToInternet函数:

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.isConnected())    
            try {
                URL url = new URL("http://www.google.com");
                HttpURLConnection urlc = (HttpURLConnection) url
                        .openConnection();
                urlc.setConnectTimeout(3000);
                urlc.connect();
                if (urlc.getResponseCode() == 200) {
                    return true;
                }
            } catch (MalformedURLException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
    return false;
}

如何使用 Volley 实现这一目标?

最佳答案

请求引发了 NoConnection 错误。请在

中捕获错误
   @Override
   public void onErrorResponse(VolleyError volleyError) {
   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.";
   }
}

关于java - Android Volley - 检查互联网状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21011279/

相关文章:

java - 在 Java 中为 Array 中的元素分配引用时到底发生了什么?

java - 类变量初始值设定项、类的静态初始值设定项和字段初始值设定项之间的区别

Java字节码操作: Adding a method to a jdk abstract class

java - 使用 Horizo​​ntalPicker 库的问题

android - 快速滚动 recyclerView 列表时,RecyclerView 内的 NetworkImageView 有时不会加载图像。 Volley 网络响应缓慢

android - 如何在 Android Volley 中使用 setEntity?

java - 在 GWT 中使用 RequestBuilder 处理附件以响应

java - 在Android中的TouchImageView中水平和垂直滚动ImageView?

android - 无法在 API <21 上运行 UI 测试

android - 在 Android 中使用 Volley 库时数据未发布到服务器