Vettu Killi
Vettu Killi

Reputation: 43

How to check User entered URL is valid or not?

I just want to check whethere the user entered url is valid or not so far what i have tried is HTTPurl connection am getting the result but response time is very slow how to make it speed up let me post my code :

   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();
        }
    }

This is what am trying now am trying to make it with volley am getting response but don't know how to validate it can anybody helpmeout: Let me post my volley code:

          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);

can anybody tell any solution for my prob

Upvotes: 1

Views: 877

Answers (2)

Dhaval Parmar
Dhaval Parmar

Reputation: 18978

try below way, just change code in onErrorResponse method of volley request

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
                    }
                }
            });

more about http status code

Upvotes: 0

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5634

Android has pre-build Patterns for WEB_URL which you can use as:-

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.
}

Upvotes: 1

Related Questions