Skip to content

Commit

Permalink
fix googlesamples#14 run HTTP GET tasks in a separate thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashfaq Ahmad committed Jun 9, 2019
1 parent 7bd9c4d commit 74846c1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static String parseVideoUrl(String videoUrl) {
}

if(parserFound == false) {
returnUrl = videoUrl;
returnUrl = url;
}

if(SimpleHttpClient.isValidUrl(returnUrl, Util.USER_AGENT_FIREFOX)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,14 @@ public void onCreate(Bundle savedInstanceState) {
}

public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Read permission is granted");
return true;
} else {
Log.v(TAG, "Read Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Read permission is granted");
return true;
} else {
Log.v(TAG, "Read Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,108 +44,126 @@ public static boolean isValidUrl(String urlStr) {
return isValidUrl(urlStr, null);
}

public static boolean isValidUrl(String urlStr, String userAgent) {
URL url = null;
HttpURLConnection urlConnection = null;

public static boolean isValidUrl(final String urlStr, final String userAgent) {
if(urlStr == null) {
return false;
}

try {
url = new URL(urlStr);
urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setConnectTimeout(DEFAULT_TIMEOUT);
urlConnection.setRequestMethod(HTTP_GET);

if(userAgent != null) {
urlConnection.setRequestProperty("User-Agent", userAgent);
final boolean[] executed = {false};
final boolean[] response = {false};
final long startTime = System.currentTimeMillis();

new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setConnectTimeout(DEFAULT_TIMEOUT);
urlConnection.setRequestMethod(HTTP_GET);

if(userAgent != null) {
urlConnection.setRequestProperty("User-Agent", userAgent);
}

if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
response[0] = true;
}
} catch (IOException e) {
Log.e(TAG, "isValidUrl: " + e.getMessage());
} finally {
executed[0] = true;
}
}
}).start();

if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return true;
while (((System.currentTimeMillis() - startTime) <= DEFAULT_TIMEOUT)) {
if (executed[0] == true || (System.currentTimeMillis() - startTime) >= DEFAULT_TIMEOUT) {
return response[0];
}
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
Log.e(TAG, "isValidUrl: " + e.getMessage());
}

return false;

}


private static String execute(String urlStr,
String httpMethod,
String userAgent,
String queryParams,
int timeout) throws IOException
private static String execute(final String urlStr,
final String httpMethod,
final String userAgent,
final String queryParams,
final int timeout) throws IOException
{
URL url = null;
HttpURLConnection urlConnection = null;
InputStream inStream = null;
OutputStream outStream = null;
String response = null;

if(urlStr == null) {
return null;
}

try
{
url = new URL(urlStr);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(timeout);
urlConnection.setRequestMethod(httpMethod);

if(userAgent != null) {
urlConnection.setRequestProperty("User-Agent", userAgent);
final String[] response = {null};
final boolean[] executed = {false};
final long startTime = System.currentTimeMillis();

new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlStr);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(timeout);
urlConnection.setReadTimeout(timeout);
urlConnection.setRequestMethod(httpMethod);

if (userAgent != null) {
urlConnection.setRequestProperty("User-Agent", userAgent);
}

if (httpMethod.contentEquals(HTTP_POST) && queryParams != null) {
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream outStream = new BufferedOutputStream(urlConnection.getOutputStream());

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
writer.write(queryParams);
writer.flush();
writer.close();
outStream.close();

urlConnection.connect();
}

int responseCode = urlConnection.getResponseCode();

if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream inStream = new BufferedInputStream(urlConnection.getInputStream());
response[0] = getInput(inStream);
} else {
response[0] = null;
}
} catch (Exception e) {
Log.e(TAG, "error getting url: " + urlStr + " with message: " + e.getMessage());
} finally {
executed[0] = true;
}
}
}).start();

if(httpMethod.contentEquals(HTTP_POST) && queryParams != null) {
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
outStream = new BufferedOutputStream(urlConnection.getOutputStream());

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
writer.write(queryParams);
writer.flush();
writer.close();
outStream.close();

urlConnection.connect();
while (((System.currentTimeMillis() - startTime) <= timeout)) {
if (executed[0] == true || (System.currentTimeMillis() - startTime) >= timeout) {
return response[0];
}

int responseCode=urlConnection.getResponseCode();

if (responseCode == HttpsURLConnection.HTTP_OK) {
inStream = new BufferedInputStream(urlConnection.getInputStream());
response = getInput(inStream);
} else {
response="";
}
} catch (Exception e) {
if(urlConnection != null) {
inStream = new BufferedInputStream(urlConnection.getInputStream());
response = getInput(inStream);
}
}
finally
{
if(urlConnection != null && urlConnection.getErrorStream() != null)
{
String errorResponse = " : ";
errorResponse = errorResponse + getInput(urlConnection.getErrorStream());
response = response + errorResponse;
}

if (urlConnection != null)
{
urlConnection.disconnect();
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}

return response;
return response[0];
}

private static String getInput(InputStream in) throws IOException
Expand Down
5 changes: 1 addition & 4 deletions iptv/iptv_channels.m3u8
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ http://z5ams.akamaized.net/zcinemasd/tracks-v1a1/index.m3u8
http://z5ams.akamaized.net/andprivehd/tracks-v1a1/index.m3u8

#EXTINF:0 tvg-logo="https://upload.wikimedia.org/wikipedia/en/thumb/1/12/%26flix_logo.png/220px-%26flix_logo.png" tvg-epg="https://tvwish.com/Channels/Flix-HD/Schedule/699", & Flix HD
http://z5ams.akamaized.net/andflixhd/tracks-v1a1/index.m3u8

#EXTINF:0 tvg-logo="https://upload.wikimedia.org/wikipedia/en/thumb/1/12/%26flix_logo.png/220px-%26flix_logo.png" tvg-epg="https://tvwish.com/Channels/Flix-HD/Schedule/700", & Flix
http://z5ams.akamaized.net/andflix/tracks-v1a1/index.m3u8
http://z5ams.akamaized.net/andflixhd/tracks-v1a1/index.m3u8;http://z5ams.akamaized.net/andflix/tracks-v1a1/index.m3u8

#EXTINF:0 tvg-logo="https://upload.wikimedia.org/wikipedia/commons/8/85/Sony_Movie_Channel.png", Sony Movies
http://yipcontent-lh.akamaihd.net/i/sonymoviechannel_1@569927/master.m3u8
Expand Down

0 comments on commit 74846c1

Please sign in to comment.