Skip to content

Commit 74846c1

Browse files
author
Ashfaq Ahmad
committed
fix googlesamples#14 run HTTP GET tasks in a separate thread
1 parent 7bd9c4d commit 74846c1

File tree

4 files changed

+104
-95
lines changed

4 files changed

+104
-95
lines changed

app/src/main/java/com/example/android/sampletvinput/parsers/TvUrlParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static String parseVideoUrl(String videoUrl) {
2929
}
3030

3131
if(parserFound == false) {
32-
returnUrl = videoUrl;
32+
returnUrl = url;
3333
}
3434

3535
if(SimpleHttpClient.isValidUrl(returnUrl, Util.USER_AGENT_FIREFOX)) {

app/src/main/java/com/example/android/sampletvinput/rich/RichTvInputSetupActivity.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,14 @@ public void onCreate(Bundle savedInstanceState) {
4444
}
4545

4646
public boolean isStoragePermissionGranted() {
47-
if (Build.VERSION.SDK_INT >= 23) {
48-
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
49-
== PackageManager.PERMISSION_GRANTED) {
50-
Log.v(TAG,"Read permission is granted");
51-
return true;
52-
} else {
53-
Log.v(TAG, "Read Permission is revoked");
54-
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
55-
return false;
56-
}
57-
}
58-
else { //permission is automatically granted on sdk<23 upon installation
59-
Log.v(TAG,"Permission is granted");
47+
if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)
48+
== PackageManager.PERMISSION_GRANTED) {
49+
Log.v(TAG,"Read permission is granted");
6050
return true;
51+
} else {
52+
Log.v(TAG, "Read Permission is revoked");
53+
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
54+
return false;
6155
}
6256
}
6357

app/src/main/java/com/example/android/sampletvinput/util/SimpleHttpClient.java

Lines changed: 95 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -44,108 +44,126 @@ public static boolean isValidUrl(String urlStr) {
4444
return isValidUrl(urlStr, null);
4545
}
4646

47-
public static boolean isValidUrl(String urlStr, String userAgent) {
48-
URL url = null;
49-
HttpURLConnection urlConnection = null;
50-
47+
public static boolean isValidUrl(final String urlStr, final String userAgent) {
5148
if(urlStr == null) {
5249
return false;
5350
}
5451

55-
try {
56-
url = new URL(urlStr);
57-
urlConnection = (HttpURLConnection) url.openConnection();
58-
59-
urlConnection.setConnectTimeout(DEFAULT_TIMEOUT);
60-
urlConnection.setRequestMethod(HTTP_GET);
61-
62-
if(userAgent != null) {
63-
urlConnection.setRequestProperty("User-Agent", userAgent);
52+
final boolean[] executed = {false};
53+
final boolean[] response = {false};
54+
final long startTime = System.currentTimeMillis();
55+
56+
new Thread(new Runnable() {
57+
@Override
58+
public void run() {
59+
try {
60+
URL url = new URL(urlStr);
61+
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
62+
63+
urlConnection.setConnectTimeout(DEFAULT_TIMEOUT);
64+
urlConnection.setRequestMethod(HTTP_GET);
65+
66+
if(userAgent != null) {
67+
urlConnection.setRequestProperty("User-Agent", userAgent);
68+
}
69+
70+
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
71+
response[0] = true;
72+
}
73+
} catch (IOException e) {
74+
Log.e(TAG, "isValidUrl: " + e.getMessage());
75+
} finally {
76+
executed[0] = true;
77+
}
6478
}
79+
}).start();
6580

66-
if(urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
67-
return true;
81+
while (((System.currentTimeMillis() - startTime) <= DEFAULT_TIMEOUT)) {
82+
if (executed[0] == true || (System.currentTimeMillis() - startTime) >= DEFAULT_TIMEOUT) {
83+
return response[0];
84+
}
85+
try {
86+
Thread.sleep(50);
87+
} catch (Exception e) {
88+
e.printStackTrace();
6889
}
69-
} catch (IOException e) {
70-
Log.e(TAG, "isValidUrl: " + e.getMessage());
7190
}
7291

7392
return false;
7493

7594
}
7695

7796

78-
private static String execute(String urlStr,
79-
String httpMethod,
80-
String userAgent,
81-
String queryParams,
82-
int timeout) throws IOException
97+
private static String execute(final String urlStr,
98+
final String httpMethod,
99+
final String userAgent,
100+
final String queryParams,
101+
final int timeout) throws IOException
83102
{
84-
URL url = null;
85-
HttpURLConnection urlConnection = null;
86-
InputStream inStream = null;
87-
OutputStream outStream = null;
88-
String response = null;
89-
90103
if(urlStr == null) {
91104
return null;
92105
}
93106

94-
try
95-
{
96-
url = new URL(urlStr);
97-
urlConnection = (HttpURLConnection) url.openConnection();
98-
urlConnection.setConnectTimeout(timeout);
99-
urlConnection.setRequestMethod(httpMethod);
100-
101-
if(userAgent != null) {
102-
urlConnection.setRequestProperty("User-Agent", userAgent);
107+
final String[] response = {null};
108+
final boolean[] executed = {false};
109+
final long startTime = System.currentTimeMillis();
110+
111+
new Thread(new Runnable() {
112+
@Override
113+
public void run() {
114+
try {
115+
URL url = new URL(urlStr);
116+
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
117+
urlConnection.setConnectTimeout(timeout);
118+
urlConnection.setReadTimeout(timeout);
119+
urlConnection.setRequestMethod(httpMethod);
120+
121+
if (userAgent != null) {
122+
urlConnection.setRequestProperty("User-Agent", userAgent);
123+
}
124+
125+
if (httpMethod.contentEquals(HTTP_POST) && queryParams != null) {
126+
urlConnection.setDoInput(true);
127+
urlConnection.setDoOutput(true);
128+
OutputStream outStream = new BufferedOutputStream(urlConnection.getOutputStream());
129+
130+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
131+
writer.write(queryParams);
132+
writer.flush();
133+
writer.close();
134+
outStream.close();
135+
136+
urlConnection.connect();
137+
}
138+
139+
int responseCode = urlConnection.getResponseCode();
140+
141+
if (responseCode == HttpsURLConnection.HTTP_OK) {
142+
InputStream inStream = new BufferedInputStream(urlConnection.getInputStream());
143+
response[0] = getInput(inStream);
144+
} else {
145+
response[0] = null;
146+
}
147+
} catch (Exception e) {
148+
Log.e(TAG, "error getting url: " + urlStr + " with message: " + e.getMessage());
149+
} finally {
150+
executed[0] = true;
151+
}
103152
}
153+
}).start();
104154

105-
if(httpMethod.contentEquals(HTTP_POST) && queryParams != null) {
106-
urlConnection.setDoInput(true);
107-
urlConnection.setDoOutput(true);
108-
outStream = new BufferedOutputStream(urlConnection.getOutputStream());
109-
110-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
111-
writer.write(queryParams);
112-
writer.flush();
113-
writer.close();
114-
outStream.close();
115-
116-
urlConnection.connect();
155+
while (((System.currentTimeMillis() - startTime) <= timeout)) {
156+
if (executed[0] == true || (System.currentTimeMillis() - startTime) >= timeout) {
157+
return response[0];
117158
}
118-
119-
int responseCode=urlConnection.getResponseCode();
120-
121-
if (responseCode == HttpsURLConnection.HTTP_OK) {
122-
inStream = new BufferedInputStream(urlConnection.getInputStream());
123-
response = getInput(inStream);
124-
} else {
125-
response="";
126-
}
127-
} catch (Exception e) {
128-
if(urlConnection != null) {
129-
inStream = new BufferedInputStream(urlConnection.getInputStream());
130-
response = getInput(inStream);
131-
}
132-
}
133-
finally
134-
{
135-
if(urlConnection != null && urlConnection.getErrorStream() != null)
136-
{
137-
String errorResponse = " : ";
138-
errorResponse = errorResponse + getInput(urlConnection.getErrorStream());
139-
response = response + errorResponse;
140-
}
141-
142-
if (urlConnection != null)
143-
{
144-
urlConnection.disconnect();
159+
try {
160+
Thread.sleep(50);
161+
} catch (Exception e) {
162+
e.printStackTrace();
145163
}
146164
}
147165

148-
return response;
166+
return response[0];
149167
}
150168

151169
private static String getInput(InputStream in) throws IOException

iptv/iptv_channels.m3u8

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ http://z5ams.akamaized.net/zcinemasd/tracks-v1a1/index.m3u8
119119
http://z5ams.akamaized.net/andprivehd/tracks-v1a1/index.m3u8
120120

121121
#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
122-
http://z5ams.akamaized.net/andflixhd/tracks-v1a1/index.m3u8
123-
124-
#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
125-
http://z5ams.akamaized.net/andflix/tracks-v1a1/index.m3u8
122+
http://z5ams.akamaized.net/andflixhd/tracks-v1a1/index.m3u8;http://z5ams.akamaized.net/andflix/tracks-v1a1/index.m3u8
126123

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

0 commit comments

Comments
 (0)