Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/backend/networkTools.java
Show All 12 Lines | BusTO - Arrival times for Turin public transports. | ||||
GNU General Public License for more details. | GNU General Public License for more details. | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.backend; | package it.reyboz.bustorino.backend; | ||||
import android.content.Context; | |||||
import androidx.annotation.Nullable; | import androidx.annotation.Nullable; | ||||
import android.util.Log; | import android.util.Log; | ||||
import java.io.BufferedInputStream; | import java.io.*; | ||||
import java.io.BufferedReader; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import java.io.InputStreamReader; | |||||
import java.net.HttpURLConnection; | import java.net.HttpURLConnection; | ||||
import java.net.URL; | import java.net.URL; | ||||
import java.util.ArrayList; | |||||
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) { | 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; | ||||
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; | ||||
Show All 20 Lines | public static String getDOM(final URL url, final AtomicReference<Fetcher.Result> res) { | ||||
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(); | ||||
} | } | ||||
public static Fetcher.Result saveFileInCache(File outputFile, URL url) { | |||||
HttpURLConnection urlConnection; | |||||
try { | |||||
urlConnection = (HttpURLConnection) url.openConnection(); | |||||
} catch (IOException e) { | |||||
//e.printStackTrace(); | |||||
return Fetcher.Result.CONNECTION_ERROR; | |||||
} | |||||
urlConnection.setConnectTimeout(4000); | |||||
urlConnection.setReadTimeout(50 * 1000); | |||||
Log.d("BusTO net Tools", "Download file "+url); | |||||
try (InputStream inputStream = urlConnection.getInputStream()) { | |||||
//File outputFile = new File(con.getFilesDir(), fileName); | |||||
//BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); | |||||
FileOutputStream outputStream = new FileOutputStream(outputFile); | |||||
byte buffer[] = new byte[16384]; | |||||
boolean inProgress = true; | |||||
while(inProgress){ | |||||
int numread = inputStream.read(buffer); | |||||
inProgress = (numread > 0); | |||||
if(inProgress) outputStream.write(buffer, 0, numread); | |||||
} | |||||
outputStream.close(); | |||||
//while (bufferedInputStream.available()) | |||||
} catch (IOException e) { | |||||
e.printStackTrace(); | |||||
try { | |||||
final Fetcher.Result res; | |||||
if(urlConnection.getResponseCode()==404) | |||||
res= Fetcher.Result.SERVER_ERROR_404; | |||||
else if(urlConnection.getResponseCode()!=200) | |||||
res= Fetcher.Result.SERVER_ERROR; | |||||
else res= Fetcher.Result.PARSER_ERROR; | |||||
urlConnection.disconnect(); | |||||
return res; | |||||
} catch (IOException ioException) { | |||||
ioException.printStackTrace(); | |||||
urlConnection.disconnect(); | |||||
return Fetcher.Result.PARSER_ERROR; | |||||
} | |||||
} | |||||
urlConnection.disconnect(); | |||||
return Fetcher.Result.OK; | |||||
} | |||||
@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