This is driving me insane. I am trying to do a very simple GET request to the Imgur API to get the images from an album as per their API https://apidocs.imgur.com/#3606f862-8281-48f1-b0f7-49a5f77da0e1
It works perfectly well in Postman, but whatever method I try to do a GET request in Android I always get a 404 page. An actual HTML page at that, not an API formatted JSON response. Here is the code I have been running inside an AsyncTask.
This is the version with OkHttp:
OkHttpClient client = new OkHttpClient();
Request request = null;
try {
request = new Request.Builder()
.url(new URL("https://api.imgur.com/3/album/6BUjs/images?v=4"))
.method("GET", null)
.addHeader("Authorization", "Client-ID " + clientId)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
.build();
} catch (MalformedURLException e) {
Log.e("OKHttpError", e.getMessage());
return null;
}
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
Log.e("OKHttpError", e.getMessage());
return null;
}
This is the version with Unirest
HttpResponse<String> response = null;
Unirest.setTimeouts(0, 0);
Unirest.clearDefaultHeaders();
try {
response = Unirest.get("https://api.imgur.com/3/album/6BUjs/images?v=14")
.header("Authorization", ("Client-ID " + clientId))
.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
.header("Content-Type", "application/json")
.header("Accept","application/json")
.asString();
} catch (UnirestException e) {
Log.e("HTTP Exception", e.getMessage());
}
return response.getBody();
And this is the version with HttpURLConnection
try {
URL url = new URL("https://api.imgur.com/3/album/6BUjs/images");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("GET");
http.setDoOutput(true);
http.setRequestProperty("Authorization", "Client-ID " + clientId);
http.setRequestProperty("Accept", "application/json");
http.setRequestProperty("Content-Type", "application/json");
http.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
return http.getResponseCode() + " " + http.getResponseMessage();
} catch (IOException e) {
e.printStackTrace();
return null;
}
Here is the full class file for reference:
public class ImgurAlbumAPIClass extends AsyncTask<String, Void, String> {
String clientId = "5070e3bc69f6159";
@Override
protected String doInBackground(String... strings) {
OkHttpClient client = new OkHttpClient();
Request request = null;
try {
request = new Request.Builder()
.url(new URL("https://api.imgur.com/3/album/6BUjs/images?v=4"))
.method("GET", null)
.addHeader("Authorization", "Client-ID " + clientId)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
.build();
} catch (MalformedURLException e) {
Log.e("OKHttpError", e.getMessage());
return null;
}
try {
Response response = client.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
Log.e("OKHttpError", e.getMessage());
return null;
}
}
@Override
protected void onPostExecute(String body) {
Log.d("ImgurAPI", body);
}
}
It really is a very simple class with a very simple goal. I have been rocking my brain. Any help is appreciated!!!