I tried to get a direct url by image, which I uploading to imgbb.com with POST
It's a json, which I should to get when I send a post to imgbb https://api.imgbb.com/
{
"data": {
"id": "2ndCYJK",
"title": "c1f64245afb2",
"url_viewer": "https://ibb.co/2ndCYJK",
"url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
"display_url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
"size": "42",
"time": "1552042565",
"expiration":"0",
"image": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
},
"thumb": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/2ndCYJK/c1f64245afb2.gif",
},
"medium": {
"filename": "c1f64245afb2.gif",
"name": "c1f64245afb2",
"mime": "image/gif",
"extension": "gif",
"url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
},
"delete_url": "https://ibb.co/2ndCYJK/670a7e48ddcb85ac340c717a41047e5c"
},
"success": true,
"status": 200
}
I tried to creat a POJO but my pojo converter says to me that it's an incorect JSON. When I try to get a response body, I can't find the url from there.
My code is:
in MainActivity
private void uploadToServer(Bitmap bitmap, Context context) throws IOException {
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] bitmapData = bos.toByteArray();
//create file
String currentDate = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault()).format(new Date());
File f = new File(context.getCacheDir(), "temp_"+currentDate );
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapData);
fos.flush();
fos.close();
Retrofit retrofit = RetroClient.getRetrofitClient(context);
UploadApi uploadAPIs = retrofit.create(UploadApi.class);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), f);
MultipartBody.Part body = MultipartBody.Part.createFormData("image", f.getName(), requestFile);
Call<ResponseBody> call = uploadAPIs.uploadImage(RetroClient.KEY_API,body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
Toast.makeText(context, "I send photo", Toast.LENGTH_SHORT);
response.body();
}
else {
Toast.makeText(context, "response isn't successful", Toast.LENGTH_SHORT);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(context, "on failure", Toast.LENGTH_SHORT);
}
});
}
Interface
public interface UploadApi {
@Multipart()
@POST("/1/upload?expiration=100&key=")
Call<ResponseBody> uploadImage(@Query("key") String key, @Part() MultipartBody.Part file );
}