Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/networkTools.java
Show All 27 Lines | |||||
import java.io.InputStreamReader; | import java.io.InputStreamReader; | ||||
import java.net.HttpURLConnection; | import java.net.HttpURLConnection; | ||||
import java.net.URL; | import java.net.URL; | ||||
import java.util.Map; | import java.util.Map; | ||||
import java.util.Scanner; | import java.util.Scanner; | ||||
import java.util.concurrent.atomic.AtomicReference; | import java.util.concurrent.atomic.AtomicReference; | ||||
public abstract class networkTools { | public abstract class networkTools { | ||||
static String getDOM(final URL url, final AtomicReference<Fetcher.result> res) { | static String getDOM(final URL url, final AtomicReference<Fetcher.Result> res) { | ||||
//Log.d("asyncwget", "Catching URL in background: " + uri[0]); | //Log.d("asyncwget", "Catching URL in background: " + uri[0]); | ||||
HttpURLConnection urlConnection; | HttpURLConnection urlConnection; | ||||
StringBuilder result = null; | StringBuilder result = null; | ||||
try { | try { | ||||
urlConnection = (HttpURLConnection) url.openConnection(); | urlConnection = (HttpURLConnection) url.openConnection(); | ||||
} catch(IOException e) { | } catch(IOException e) { | ||||
res.set(Fetcher.result.SERVER_ERROR); | res.set(Fetcher.Result.SERVER_ERROR); | ||||
return null; | return null; | ||||
} | } | ||||
try { | try { | ||||
InputStream in = new BufferedInputStream( | InputStream in = new BufferedInputStream( | ||||
urlConnection.getInputStream()); | urlConnection.getInputStream()); | ||||
BufferedReader reader = new BufferedReader( | BufferedReader reader = new BufferedReader( | ||||
new InputStreamReader(in)); | new InputStreamReader(in)); | ||||
result = new StringBuilder(); | result = new StringBuilder(); | ||||
String line; | String line; | ||||
while ((line = reader.readLine()) != null) { | while ((line = reader.readLine()) != null) { | ||||
result.append(line); | result.append(line); | ||||
} | } | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
//Log.e("asyncwget", e.getMessage()); | //Log.e("asyncwget", e.getMessage()); | ||||
} finally { | } finally { | ||||
if (urlConnection != null) { | if (urlConnection != null) { | ||||
urlConnection.disconnect(); | urlConnection.disconnect(); | ||||
} | } | ||||
} | } | ||||
if (result == null) { | if (result == null) { | ||||
res.set(Fetcher.result.SERVER_ERROR); | res.set(Fetcher.Result.SERVER_ERROR); | ||||
return null; | return null; | ||||
} | } | ||||
res.set(Fetcher.result.PARSER_ERROR); // will be set to "OK" later, this is a safety net in case StringBuilder returns null, the website returns an HTTP 204 or something like that. | res.set(Fetcher.Result.PARSER_ERROR); // will be set to "OK" later, this is a safety net in case StringBuilder returns null, the website returns an HTTP 204 or something like that. | ||||
return result.toString(); | return result.toString(); | ||||
} | } | ||||
@Nullable | @Nullable | ||||
static String queryURL(URL url, AtomicReference<Fetcher.result> res){ | static String queryURL(URL url, AtomicReference<Fetcher.Result> res){ | ||||
return queryURL(url,res,null); | return queryURL(url,res,null); | ||||
} | } | ||||
@Nullable | @Nullable | ||||
static String queryURL(URL url, AtomicReference<Fetcher.result> res, Map<String,String> headers) { | static String queryURL(URL url, AtomicReference<Fetcher.Result> res, Map<String,String> headers) { | ||||
HttpURLConnection urlConnection; | HttpURLConnection urlConnection; | ||||
InputStream in; | InputStream in; | ||||
String s; | String s; | ||||
try { | try { | ||||
urlConnection = (HttpURLConnection) url.openConnection(); | urlConnection = (HttpURLConnection) url.openConnection(); | ||||
} catch(IOException e) { | } catch(IOException e) { | ||||
//e.printStackTrace(); | //e.printStackTrace(); | ||||
res.set(Fetcher.result.SERVER_ERROR); // even when offline, urlConnection works fine. WHY. | res.set(Fetcher.Result.SERVER_ERROR); // even when offline, urlConnection works fine. WHY. | ||||
return null; | return null; | ||||
} | } | ||||
// TODO: make this configurable? | // TODO: make this configurable? | ||||
urlConnection.setConnectTimeout(3000); | urlConnection.setConnectTimeout(3000); | ||||
urlConnection.setReadTimeout(10000); | urlConnection.setReadTimeout(10000); | ||||
if(headers!= null){ | if(headers!= null){ | ||||
for(String key : headers.keySet()){ | for(String key : headers.keySet()){ | ||||
urlConnection.setRequestProperty(key,headers.get(key)); | urlConnection.setRequestProperty(key,headers.get(key)); | ||||
} | } | ||||
} | } | ||||
res.set(Fetcher.result.SERVER_ERROR); // will be set to OK later | res.set(Fetcher.Result.SERVER_ERROR); // will be set to OK later | ||||
try { | try { | ||||
in = urlConnection.getInputStream(); | in = urlConnection.getInputStream(); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
try { | try { | ||||
if(urlConnection.getResponseCode()==404) | if(urlConnection.getResponseCode()==404) | ||||
res.set(Fetcher.result.SERVER_ERROR_404); | res.set(Fetcher.Result.SERVER_ERROR_404); | ||||
} catch (IOException e2) { | } catch (IOException e2) { | ||||
e2.printStackTrace(); | e2.printStackTrace(); | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
//s = streamToString(in); | //s = streamToString(in); | ||||
try { | try { | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register