@POST("/3/image")ImageResponse postImage (
@Header("X-Mashape-Key")String key,
@Header("Authorization")String auth,
@Header("Type")String type,
@Query("title")String title,
@Query("description")String description,
@Query("album")String albumId,
@Query("account_url")String username,
@Body TypedFile file
response = imgurAdapter.create(ImgurAPI.class)
.postImage(
Const.getMashapeAuth(), Const.getClientAuth(), type, title, description, albumId, null, new TypedFile("image/*", image)
);
public static String getMashapeAuth() {
return "X-Mashape-Key: " + X_MASHAPE_KEY;
}
Unirest method:
HttpResponse<JsonNode> response = Unirest.post("https://imgur-apiv3.p.mashape.com/3/image")
.header("X-Mashape-Key", Const.X_MASHAPE_KEY)
.header("Authorization", Const.getClientAuth())
.header("Content-Type", "application/x-www-form-urlencoded")
.field("album", albumId)
.field("description", description)
.field("image", image)
.field("title", title)
.asJson();
@FormUrlEncoded
@POST("/3/image/")ImageResponse postImage (
@Header("X-Mashape-Key")String key,// header seems correct type. otherwise 401 auth error
@Header("Authorization")String auth,
@Header("type")String type, // unsure if this is necessary
@Field("title")String title,
@Field("description")String description,
@Field("album")String albumId,
@Field("account_url")String username,
@Field("image") TypedFile image // unsure on what type of files are acceptable. they are natively .jpg
Log cat like this:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 D/Retrofit﹕ Content-Length: 133 D/Retrofit﹕ title=h&description=Get+the+snicKer+app+isnicker.com&image=%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20150630_170632739_HDR.jpg D/Retrofit﹕ ---> END HTTP (133-byte body)
I'll email support again for more help.
anythoughts?
Post is:
@FormUrlEncoded
@POST("/3/image/")ImageResponse postImage (
@Header("X-Mashape-Key")String key,
@Header("Authorization")String auth,
@Header("Content-Type")String type,
@Field("title")String title,
@Field("description")String description,
@Field("album")String albumId,
@Field("account_url")String username,
@Field("image") String file
requested like:
response = imgurAdapter.create(ImgurAPI.class)
.postImage(
Const.MY_X_KEY_MASHAPE, Const.getClientAuth(),Const.type, title, description, albumId, null , image64
);
And the BIG THING:
convert image to base64 string
try {
InputStream inputStream = new FileInputStream(image);//You can get an inputStream using any IO API
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
String image64 = Base64.encodeToString(bytes, Base64.DEFAULT);
}
catch(Exception e) {
e.printStackTrace();
Log.i(TAG, "Image file not available");
}
Hope that saves someone some time