Created
July 3, 2017 11:22
-
-
Save hector6872/36100ab0ae4c4b4de2a0d47233bbbde5 to your computer and use it in GitHub Desktop.
Network
This file contains hidden or 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
public final class Network { | |
private Network() { | |
} | |
public static @NonNull String doGetRequest(@NonNull String service, @NonNull String resource, | |
@NonNull List<Pair<String, String>> params, @NonNull List<Pair<String, String>> headers) throws IOException { | |
HttpURLConnection httpURLConnection = null; | |
try { | |
URL url = new URL( | |
service + "/" + Uri.encode(resource) + (params.isEmpty() ? "" : "&" + convertPairToParamString(params))); | |
httpURLConnection = (HttpURLConnection) url.openConnection(); | |
httpURLConnection.setRequestMethod("GET"); | |
makeHeaders(httpURLConnection, headers); | |
httpURLConnection.connect(); | |
return convertStreamToString(httpURLConnection.getInputStream()); | |
} catch (Exception e) { | |
throw new IOException(); | |
} finally { | |
if (httpURLConnection != null) { | |
httpURLConnection.disconnect(); | |
} | |
} | |
} | |
public static @NonNull String doPostRequest(@NonNull String service, @NonNull String resource, | |
@NonNull List<Pair<String, String>> params, @NonNull List<Pair<String, String>> headers) throws IOException { | |
HttpURLConnection httpURLConnection = null; | |
try { | |
URL url = new URL(service + "/" + Uri.encode(resource)); | |
httpURLConnection = (HttpURLConnection) url.openConnection(); | |
httpURLConnection.setRequestMethod("POST"); | |
makeHeaders(httpURLConnection, headers); | |
OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream()); | |
writer.write(convertPairToParamString(params)); | |
writer.close(); | |
httpURLConnection.connect(); | |
return convertStreamToString(httpURLConnection.getInputStream()); | |
} catch (Exception e) { | |
throw new IOException(); | |
} finally { | |
if (httpURLConnection != null) { | |
httpURLConnection.disconnect(); | |
} | |
} | |
} | |
private static void makeHeaders(@NonNull HttpURLConnection httpURLConnection, | |
@NonNull List<Pair<String, String>> headers) { | |
for (Pair<String, String> pair : headers) { | |
httpURLConnection.setRequestProperty(pair.first, pair.second); | |
} | |
} | |
private static @NonNull String convertPairToParamString(List<Pair<String, String>> params) { | |
StringBuilder result = new StringBuilder(); | |
boolean first = true; | |
for (Pair<String, String> pair : params) { | |
if (first) { | |
first = false; | |
} else { | |
result.append("&"); | |
} | |
result.append(Uri.encode(pair.first)); | |
result.append("="); | |
result.append(Uri.encode(pair.second)); | |
} | |
return result.toString(); | |
} | |
private static @NonNull String convertStreamToString(@NonNull InputStream inputStream) throws IOException { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String line; | |
try { | |
while ((line = bufferedReader.readLine()) != null) { | |
stringBuilder.append(line).append("\n"); | |
} | |
} catch (IOException e) { | |
throw new IOException(); | |
} finally { | |
try { | |
inputStream.close(); | |
} catch (IOException ignored) { | |
} | |
} | |
return stringBuilder.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment