This repository has been archived by the owner on Oct 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Renamed UpdateRequest to UpdateDownloadRequest. Added helper class fo…
…r URLConnection. Adding headers to all requests, if they are set. #153
- Loading branch information
1 parent
5001511
commit 210eb6f
Showing
4 changed files
with
79 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/android/src/com/nordnetab/chcp/main/utils/URLConnectionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.nordnetab.chcp.main.utils; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by Nikolay Demyankov on 03.06.16. | ||
* <p/> | ||
* Helper class to work with URLConnection | ||
*/ | ||
public class URLConnectionHelper { | ||
|
||
// connection timeout in milliseconds | ||
private static final int CONNECTION_TIMEOUT = 30000; | ||
|
||
// data read timeout in milliseconds | ||
private static final int READ_TIMEOUT = 30000; | ||
|
||
/** | ||
* Create URLConnection instance. | ||
* | ||
* @param url to what url | ||
* @param requestHeaders additional request headers | ||
* @return connection instance | ||
* @throws IOException when url is invalid or failed to establish connection | ||
*/ | ||
public static URLConnection createConnectionToURL(final String url, final Map<String, String> requestHeaders) throws IOException { | ||
final URL connectionURL = URLUtility.stringToUrl(url); | ||
if (connectionURL == null) { | ||
throw new IOException("Invalid url format: " + url); | ||
} | ||
|
||
final URLConnection urlConnection = connectionURL.openConnection(); | ||
urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); | ||
urlConnection.setReadTimeout(READ_TIMEOUT); | ||
|
||
if (requestHeaders != null) { | ||
for (final Map.Entry<String, String> entry : requestHeaders.entrySet()) { | ||
urlConnection.setRequestProperty(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
return urlConnection; | ||
} | ||
|
||
} |