Last active
September 25, 2016 18:41
-
-
Save revanmj/2f9ca524eb3587c0d067cefa9c5140c5 to your computer and use it in GitHub Desktop.
Simple class for HTTP requests in Java and Android
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
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParser; | |
class SimpleJavaHttp { | |
public static final String HTTP_GET = "GET"; | |
public static final String HTTP_POST = "POST"; | |
public static final String HTTP_DELETE = "DELETE"; | |
public static HttpResult makeHttpRequest(String method, String auth, String url_s, String body) throws IOException { | |
URL url = new URL(url_s); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setRequestProperty("Authorization", "Basic " + auth); | |
connection.setRequestProperty("Connection", "close"); | |
connection.setConnectTimeout(6000); | |
connection.setRequestMethod(method); | |
if (body != null) { | |
connection.setRequestProperty("Content-Type", "application/json"); | |
connection.setRequestProperty("Accept", "application/json"); | |
connection.setDoOutput(true); | |
connection.setDoInput(true); | |
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); | |
wr.write(body); | |
wr.flush(); | |
} | |
int responseCode = connection.getResponseCode(); | |
if (responseCode >= 400) | |
return new HttpResult(responseCode, null); | |
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); | |
CrLog("makeHttpRequest", method + ": url[" + url_s + "], responseCode[" + responseCode + "] body[" + body + "]"); | |
StringBuilder result = new StringBuilder(""); | |
String line = ""; | |
while ((line = br.readLine()) != null) { | |
result.append(line); | |
} | |
br.close(); | |
connection.disconnect(); | |
if (result.length() != 0) | |
return new HttpResult(responseCode, result.toString()); | |
else | |
return new HttpResult(responseCode, null); | |
} | |
public static class HttpResult { | |
private int responseCode; | |
private String result; | |
public HttpResult(int code, String r) { | |
responseCode = code; | |
result = r; | |
} | |
public String getResult() { | |
return result; | |
} | |
public int getResponseCode() { | |
return responseCode; | |
} | |
public JsonArray getAsJsonArray() { | |
JsonElement element = new JsonParser().parse(result); | |
if (result != null && element.isJsonArray()) | |
return element.getAsJsonArray(); | |
else | |
return null; | |
} | |
public JsonObject getAsJsonObject() { | |
JsonElement element = new JsonParser().parse(result); | |
if (result != null && element.isJsonObject()) | |
return element.getAsJsonObject(); | |
else | |
return null; | |
} | |
@Override | |
public String toString() { | |
return "responseCode[" + responseCode + "] responseBody[" + result + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment