@@ -44,108 +44,126 @@ public static boolean isValidUrl(String urlStr) {
4444 return isValidUrl (urlStr , null );
4545 }
4646
47- public static boolean isValidUrl (String urlStr , String userAgent ) {
48- URL url = null ;
49- HttpURLConnection urlConnection = null ;
50-
47+ public static boolean isValidUrl (final String urlStr , final String userAgent ) {
5148 if (urlStr == null ) {
5249 return false ;
5350 }
5451
55- try {
56- url = new URL (urlStr );
57- urlConnection = (HttpURLConnection ) url .openConnection ();
58-
59- urlConnection .setConnectTimeout (DEFAULT_TIMEOUT );
60- urlConnection .setRequestMethod (HTTP_GET );
61-
62- if (userAgent != null ) {
63- urlConnection .setRequestProperty ("User-Agent" , userAgent );
52+ final boolean [] executed = {false };
53+ final boolean [] response = {false };
54+ final long startTime = System .currentTimeMillis ();
55+
56+ new Thread (new Runnable () {
57+ @ Override
58+ public void run () {
59+ try {
60+ URL url = new URL (urlStr );
61+ HttpURLConnection urlConnection = (HttpURLConnection ) url .openConnection ();
62+
63+ urlConnection .setConnectTimeout (DEFAULT_TIMEOUT );
64+ urlConnection .setRequestMethod (HTTP_GET );
65+
66+ if (userAgent != null ) {
67+ urlConnection .setRequestProperty ("User-Agent" , userAgent );
68+ }
69+
70+ if (urlConnection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
71+ response [0 ] = true ;
72+ }
73+ } catch (IOException e ) {
74+ Log .e (TAG , "isValidUrl: " + e .getMessage ());
75+ } finally {
76+ executed [0 ] = true ;
77+ }
6478 }
79+ }).start ();
6580
66- if (urlConnection .getResponseCode () == HttpURLConnection .HTTP_OK ) {
67- return true ;
81+ while (((System .currentTimeMillis () - startTime ) <= DEFAULT_TIMEOUT )) {
82+ if (executed [0 ] == true || (System .currentTimeMillis () - startTime ) >= DEFAULT_TIMEOUT ) {
83+ return response [0 ];
84+ }
85+ try {
86+ Thread .sleep (50 );
87+ } catch (Exception e ) {
88+ e .printStackTrace ();
6889 }
69- } catch (IOException e ) {
70- Log .e (TAG , "isValidUrl: " + e .getMessage ());
7190 }
7291
7392 return false ;
7493
7594 }
7695
7796
78- private static String execute (String urlStr ,
79- String httpMethod ,
80- String userAgent ,
81- String queryParams ,
82- int timeout ) throws IOException
97+ private static String execute (final String urlStr ,
98+ final String httpMethod ,
99+ final String userAgent ,
100+ final String queryParams ,
101+ final int timeout ) throws IOException
83102 {
84- URL url = null ;
85- HttpURLConnection urlConnection = null ;
86- InputStream inStream = null ;
87- OutputStream outStream = null ;
88- String response = null ;
89-
90103 if (urlStr == null ) {
91104 return null ;
92105 }
93106
94- try
95- {
96- url = new URL (urlStr );
97- urlConnection = (HttpURLConnection ) url .openConnection ();
98- urlConnection .setConnectTimeout (timeout );
99- urlConnection .setRequestMethod (httpMethod );
100-
101- if (userAgent != null ) {
102- urlConnection .setRequestProperty ("User-Agent" , userAgent );
107+ final String [] response = {null };
108+ final boolean [] executed = {false };
109+ final long startTime = System .currentTimeMillis ();
110+
111+ new Thread (new Runnable () {
112+ @ Override
113+ public void run () {
114+ try {
115+ URL url = new URL (urlStr );
116+ HttpURLConnection urlConnection = (HttpURLConnection ) url .openConnection ();
117+ urlConnection .setConnectTimeout (timeout );
118+ urlConnection .setReadTimeout (timeout );
119+ urlConnection .setRequestMethod (httpMethod );
120+
121+ if (userAgent != null ) {
122+ urlConnection .setRequestProperty ("User-Agent" , userAgent );
123+ }
124+
125+ if (httpMethod .contentEquals (HTTP_POST ) && queryParams != null ) {
126+ urlConnection .setDoInput (true );
127+ urlConnection .setDoOutput (true );
128+ OutputStream outStream = new BufferedOutputStream (urlConnection .getOutputStream ());
129+
130+ BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (outStream , "UTF-8" ));
131+ writer .write (queryParams );
132+ writer .flush ();
133+ writer .close ();
134+ outStream .close ();
135+
136+ urlConnection .connect ();
137+ }
138+
139+ int responseCode = urlConnection .getResponseCode ();
140+
141+ if (responseCode == HttpsURLConnection .HTTP_OK ) {
142+ InputStream inStream = new BufferedInputStream (urlConnection .getInputStream ());
143+ response [0 ] = getInput (inStream );
144+ } else {
145+ response [0 ] = null ;
146+ }
147+ } catch (Exception e ) {
148+ Log .e (TAG , "error getting url: " + urlStr + " with message: " + e .getMessage ());
149+ } finally {
150+ executed [0 ] = true ;
151+ }
103152 }
153+ }).start ();
104154
105- if (httpMethod .contentEquals (HTTP_POST ) && queryParams != null ) {
106- urlConnection .setDoInput (true );
107- urlConnection .setDoOutput (true );
108- outStream = new BufferedOutputStream (urlConnection .getOutputStream ());
109-
110- BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (outStream , "UTF-8" ));
111- writer .write (queryParams );
112- writer .flush ();
113- writer .close ();
114- outStream .close ();
115-
116- urlConnection .connect ();
155+ while (((System .currentTimeMillis () - startTime ) <= timeout )) {
156+ if (executed [0 ] == true || (System .currentTimeMillis () - startTime ) >= timeout ) {
157+ return response [0 ];
117158 }
118-
119- int responseCode =urlConnection .getResponseCode ();
120-
121- if (responseCode == HttpsURLConnection .HTTP_OK ) {
122- inStream = new BufferedInputStream (urlConnection .getInputStream ());
123- response = getInput (inStream );
124- } else {
125- response ="" ;
126- }
127- } catch (Exception e ) {
128- if (urlConnection != null ) {
129- inStream = new BufferedInputStream (urlConnection .getInputStream ());
130- response = getInput (inStream );
131- }
132- }
133- finally
134- {
135- if (urlConnection != null && urlConnection .getErrorStream () != null )
136- {
137- String errorResponse = " : " ;
138- errorResponse = errorResponse + getInput (urlConnection .getErrorStream ());
139- response = response + errorResponse ;
140- }
141-
142- if (urlConnection != null )
143- {
144- urlConnection .disconnect ();
159+ try {
160+ Thread .sleep (50 );
161+ } catch (Exception e ) {
162+ e .printStackTrace ();
145163 }
146164 }
147165
148- return response ;
166+ return response [ 0 ] ;
149167 }
150168
151169 private static String getInput (InputStream in ) throws IOException
0 commit comments