Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created June 25, 2022 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/febe8e1306a388cd3a44a0abc60ca612 to your computer and use it in GitHub Desktop.
Save Shilo/febe8e1306a388cd3a44a0abc60ca612 to your computer and use it in GitHub Desktop.
Android Java helper class for handling HTTP GET requests. (Requires Java 11+)
package com.shilocity.http;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Consumer;
public class HttpClientManager {
public static Future<?> sendAsync(String url) {
return sendAsync(url, null);
}
public static Future<?> sendAsync(String url, Consumer<HttpClientManagerResult> onFinished) {
ExecutorService executor = Executors.newSingleThreadExecutor();
return executor.submit(
() -> {
HttpClientManagerResult result = send(url);
if (onFinished != null)
onFinished.accept(result);
executor.shutdown();
});
}
public static HttpClientManagerResult send(String url) {
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) new URL(url).openConnection();
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
String response = readInputStream(inputStream);
urlConnection.disconnect();
return new HttpClientManagerResult(response);
} catch (Exception exception) {
if (urlConnection != null)
urlConnection.disconnect();
return new HttpClientManagerResult(exception);
}
}
public static String addressToUrl(String ip) {
return addressToUrl(ip, 80);
}
public static String addressToUrl(String ip, int port) {
return addressToUrl(ip, port, true);
}
public static String addressToUrl(String ip, int port, boolean isSecure) {
String scheme = isSecure ? "https" : "http";
String url = scheme + "://" + ip;
if (port != 80)
url += ":" + port;
return url;
}
private static String readInputStream(InputStream inputStream) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
boolean isNewLine = false;
for (String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) {
if (isNewLine)
stringBuilder.append("\n");
stringBuilder.append(line);
isNewLine = true;
}
inputStream.close();
return stringBuilder.toString();
}
}
package com.shilocity.http;
public class HttpClientManagerResult {
public String response;
public Exception exception;
public HttpClientManagerResult(String response) {
this.response = response;
this.exception = null;
}
public HttpClientManagerResult(Exception exception) {
this.response = null;
this.exception = exception;
}
}
package de.xida.myapplication;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.shilocity.http.HttpClientManager;
import com.shilocity.http.HttpClientManagerResult;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String ip = "www.google.com";
int port = 80;
boolean isSecure = true;
String url = HttpClientManager.addressToUrl(ip, port, isSecure);
exampleSendAsync(url, isSecure);
}
static void exampleSendAsync(String url, boolean isSecure) {
HttpClientManager.sendAsync(url,
(result) -> onExampleSendFinished(result));
}
static void onExampleSendFinished(HttpClientManagerResult result) {
if (result.exception != null) {
Log.w("===", result.exception);
// todo: handle error here
return;
}
Log.d("===", result.response);
// todo: handle http body here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment