Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/networkTools.java
Show All 20 Lines | |||||
import android.content.Context; | import android.content.Context; | ||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import android.util.Log; | import android.util.Log; | ||||
import java.io.*; | import java.io.*; | ||||
import java.net.HttpURLConnection; | import java.net.HttpURLConnection; | ||||
import java.net.URL; | import java.net.URL; | ||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Date; | |||||
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 { | ||||
public static String getDOM(final URL url, final AtomicReference<Fetcher.Result> res) { | public 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; | ||||
Show All 37 Lines | public static Fetcher.Result saveFileInCache(File outputFile, URL url) { | ||||
try { | try { | ||||
urlConnection = (HttpURLConnection) url.openConnection(); | urlConnection = (HttpURLConnection) url.openConnection(); | ||||
} catch (IOException e) { | } catch (IOException e) { | ||||
//e.printStackTrace(); | //e.printStackTrace(); | ||||
return Fetcher.Result.CONNECTION_ERROR; | return Fetcher.Result.CONNECTION_ERROR; | ||||
} | } | ||||
urlConnection.setConnectTimeout(4000); | urlConnection.setConnectTimeout(4000); | ||||
urlConnection.setReadTimeout(50 * 1000); | urlConnection.setReadTimeout(50 * 1000); | ||||
System.out.println("Last modified: "+new Date(urlConnection.getLastModified())); | |||||
Log.d("BusTO net Tools", "Download file "+url); | Log.d("BusTO net Tools", "Download file "+url); | ||||
try (InputStream inputStream = urlConnection.getInputStream()) { | try (InputStream inputStream = urlConnection.getInputStream()) { | ||||
//File outputFile = new File(con.getFilesDir(), fileName); | //File outputFile = new File(con.getFilesDir(), fileName); | ||||
//BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); | //BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); | ||||
FileOutputStream outputStream = new FileOutputStream(outputFile); | FileOutputStream outputStream = new FileOutputStream(outputFile); | ||||
byte buffer[] = new byte[16384]; | byte buffer[] = new byte[16384]; | ||||
boolean inProgress = true; | boolean inProgress = true; | ||||
Show All 19 Lines | public static Fetcher.Result saveFileInCache(File outputFile, URL url) { | ||||
ioException.printStackTrace(); | ioException.printStackTrace(); | ||||
urlConnection.disconnect(); | urlConnection.disconnect(); | ||||
return Fetcher.Result.PARSER_ERROR; | return Fetcher.Result.PARSER_ERROR; | ||||
} | } | ||||
} | } | ||||
urlConnection.disconnect(); | urlConnection.disconnect(); | ||||
return Fetcher.Result.OK; | return Fetcher.Result.OK; | ||||
} | } | ||||
@Nullable | |||||
public static Date checkLastModificationDate(URL url, AtomicReference<Fetcher.Result> res) { | |||||
HttpURLConnection urlConnection; | |||||
try { | |||||
urlConnection = (HttpURLConnection) url.openConnection(); | |||||
} catch (IOException e) { | |||||
//e.printStackTrace(); | |||||
res.set(Fetcher.Result.CONNECTION_ERROR); | |||||
return null; | |||||
} | |||||
urlConnection.setConnectTimeout(4000); | |||||
urlConnection.setReadTimeout(4 * 1000); | |||||
System.out.println("Last modified: "+new Date(urlConnection.getLastModified())); | |||||
Log.d("BusTO net Tools", "Download file "+url); | |||||
final Date theDate = new Date(urlConnection.getLastModified()); | |||||
try { | |||||
if(urlConnection.getResponseCode()==404) | |||||
res.set(Fetcher.Result.SERVER_ERROR_404); | |||||
else if(urlConnection.getResponseCode()!=200) | |||||
res.set(Fetcher.Result.SERVER_ERROR); | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
res.set(Fetcher.Result.PARSER_ERROR); | |||||
} | |||||
urlConnection.disconnect(); | |||||
//theDate.getTime() | |||||
return theDate; | |||||
} | |||||
@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; | ||||
▲ Show 20 Lines • Show All 100 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