【问题标题】:How to check User entered URL is valid or not?如何检查用户输入的 URL 是否有效?
【发布时间】:2016-05-09 11:39:27
【问题描述】:

我只是想检查用户输入的 url 是否有效到目前为止我尝试过的是 HTTPurl 连接正在得到结果,但响应时间很慢如何让它加快速度让我发布我的代码:

   public Boolean Netwrok(String str) {

        HttpURLConnection connection = null;
        try {
            URL myUrl = new URL(str);

            connection = (HttpURLConnection) myUrl.openConnection();
            connection.setRequestProperty("Accept-Encoding", "identity");
            InputStream response = connection.getInputStream();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            connection.disconnect();
        }
    }

这就是我现在正在尝试做的事情,我正在尝试通过 volley 来获得响应,但不知道如何验证它可以帮助任何人: 让我发布我的截击代码:

          RequestQueue queue = Volley.newRequestQueue(getContext());
                String str_url = getEditText().getText().toString();
                str_url = s;

// Request a string response from the provided URL.
                StringRequest stringRequest = new StringRequest(Request.Method.GET, s,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                // Display the first 500 characters of the response string.


                                    Intent intent = new Intent(getContext(), LoginActivity.class);
                                    getContext().startActivity(intent);



                                //    mTextView.setText("Response is: "+ response.substring(0,500));
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        setDialogMessage("That didn't work!");
                    }
                });
// Add the request to the RequestQueue.
                queue.add(stringRequest);

谁能说出我的问题的任何解决方案

【问题讨论】:

    标签: android url android-volley httpurlconnection


    【解决方案1】:

    试试下面的方法,只是在onErrorResponse凌空请求方法中更改代码

    StringRequest stringRequest = new StringRequest(Request.Method.GET, s,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
    
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //setDialogMessage("That didn't work!");
                        // NOT VALID URL
                        NetworkResponse networkResponse = error.networkResponse;
                        if (networkResponse.statusCode != 200) {
                        Log.e("Status code", String.valueOf(networkResponse.statusCode));
                        //code 404: for page not found, you can read about more responce code in link below
                        }
                    }
                });
    

    更多关于http status code

    【讨论】:

    • 那么哪种方式有效@MikeM。
    【解决方案2】:

    Android 为 WEB_URL 预构建了 Patterns,您可以将其用作:-

    if (Patterns.WEB_URL.matcher(serverURL).matches() == false){
        //Invalid
    }else{
        //valid
    
        //In here you can also put one more check by opening connection to URL and check for HTTP_RESPONSE_OK (200) then url is FINE.
    }
    

    【讨论】:

    • 如果您只进行模式匹配会很快,但对于下面提到的另一条评论有效,通过打开与 url 的连接来检查 http_response_ok 可能需要一些时间,具体取决于您的互联网连接和服务器。
    • 响应需要 4 分钟,所以我使用了 volley
    • 是的,检查 http_response_ok 取决于它只是一个建议。对于有效输入与否,您可以使用模式匹配器。
    猜你喜欢
    • 2014-06-30
    • 2014-08-08
    • 1970-01-01
    • 2013-01-06
    • 2011-09-06
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    相关资源
    最近更新 更多