Files
janasRoboter/app/src/main/java/com/janasroboter/REST.java

58 lines
1.7 KiB
Java

package com.janasroboter;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class REST {
// Initialize the http client
private static OkHttpClient client = new OkHttpClient();
// Initialize the JSON mediatype used for post requests
private static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
private static final MediaType PATCHTYPE = MediaType.parse("application/json-patch+json");
private static final MediaType MEDIA_TYPE_MP4 = MediaType.parse("video/mp4");
public static final String URL = "http://192.168.4.1";
/**
* Make a GET request to a url
* @param url the url string
* @return content of the site body
* @throws IOException
*/
public static String getString(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
/**
* Make a GET request with an authentication token
* @param url the url string
* @param authtoken string containing the apikey/authentication token
* @return content of the site body
* @throws IOException
*/
public static String getString(String url, String authtoken) throws IOException {
// Create a request with an additional authorization header
Request request = new Request.Builder().header("Authorization", authtoken)
.url(url).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
}