Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/networkTools.java
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Lines | public abstract class networkTools { | ||||
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(); | |||||
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(5000); | 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 | ||||
Show All 18 Lines | static String queryURL(URL url, AtomicReference<Fetcher.result> res, Map<String,String> headers) { | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
e.printStackTrace(); | e.printStackTrace(); | ||||
return null; | return null; | ||||
} | } | ||||
try { | try { | ||||
in.close(); | in.close(); | ||||
} catch(Exception ignored) {} | } catch(IOException ignored) { | ||||
//ignored.printStackTrace(); | |||||
} | |||||
try { | try { | ||||
urlConnection.disconnect(); | urlConnection.disconnect(); | ||||
} catch(Exception ignored) {} | } catch(Exception ignored) { | ||||
//ignored.printStackTrace(); | |||||
} | |||||
if(s.length() == 0) { | if(s.length() == 0) { | ||||
Log.w("NET TOOLS", "string is empty"); | |||||
return null; | return null; | ||||
} else { | } else { | ||||
//Log.d("NET TOOLS", s); | |||||
return s; | return s; | ||||
} | } | ||||
} | } | ||||
// https://stackoverflow.com/a/5445161 | // https://stackoverflow.com/a/5445161 | ||||
static String streamToString(InputStream is) { | static String streamToString(InputStream is) { | ||||
Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A"); | Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A"); | ||||
return s.hasNext() ? s.next() : ""; | return s.hasNext() ? s.next() : ""; | ||||
Show All 31 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