Created
March 5, 2021 16:01
-
-
Save Hopefuls/058782e830681c85986d1b37f3e146e4 to your computer and use it in GitHub Desktop.
RequestMaker
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
package me.hopedev.hopeutils.requesting; | |
import com.squareup.okhttp.*; | |
public class MakeRequest { | |
private final String URL; | |
public static OkHttpClient client = new OkHttpClient(); | |
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); | |
public MakeRequest(String requestURL) { | |
this.URL = requestURL; | |
} | |
public final Response post(String body) { | |
Response response = null; | |
try { | |
RequestBody postBody = RequestBody.create(JSON, body); | |
Request request = new Request.Builder() | |
.url(this.URL) | |
.post(postBody) | |
.build(); | |
response = client.newCall(request).execute(); | |
} catch (Exception e) { | |
System.out.println("Failed to make POST request to "+URL); | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
public final Response get() { | |
Response response = null; | |
try { | |
Request request = new Request.Builder() | |
.url(this.URL) | |
.build(); | |
response = client.newCall(request).execute(); | |
} catch (Exception e) { | |
System.out.println("Failed to make GET request to "+URL); | |
e.printStackTrace(); | |
} | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment