use of me.ccrama.redditslide.util.ProgressRequestBody in project Slide by ccrama.
the class UploadImgurAlbum method doInBackground.
@Override
protected String doInBackground(Uri... sub) {
totalCount = sub.length;
final OkHttpClient client = Reddit.client;
String albumurl;
{
Request request = new Request.Builder().header("Authorization", "Client-ID bef87913eb202e9").url("https://api.imgur.com/3/album").post(new RequestBody() {
@Override
public MediaType contentType() {
return null;
}
@Override
public void writeTo(BufferedSink sink) {
}
}).build();
Response response = null;
try {
response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
JSONObject album = new JSONObject(response.body().string());
albumurl = album.getJSONObject("data").getString("deletehash");
finalUrl = "http://imgur.com/a/" + album.getJSONObject("data").getString("id");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
try {
MultipartBody.Builder formBodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Uri uri : sub) {
File bitmap = ImgurUtils.createFile(uri, c);
formBodyBuilder.addFormDataPart("image", bitmap.getName(), RequestBody.create(MediaType.parse("image/*"), bitmap));
formBodyBuilder.addFormDataPart("album", albumurl);
MultipartBody formBody = formBodyBuilder.build();
ProgressRequestBody body = new ProgressRequestBody(formBody, this::publishProgress);
Request request = new Request.Builder().header("Authorization", "Client-ID bef87913eb202e9").url("https://api.imgur.com/3/image").post(body).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of me.ccrama.redditslide.util.ProgressRequestBody in project Slide by ccrama.
the class UploadImgur method doInBackground.
@Override
protected JSONObject doInBackground(Uri... sub) {
File bitmap = ImgurUtils.createFile(sub[0], c);
final OkHttpClient client = Reddit.client;
try {
RequestBody formBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("image", bitmap.getName(), RequestBody.create(MediaType.parse("image/*"), bitmap)).build();
ProgressRequestBody body = new ProgressRequestBody(formBody, this::publishProgress);
Request request = new Request.Builder().header("Authorization", "Client-ID bef87913eb202e9").url("https://api.imgur.com/3/image").post(body).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
return new JSONObject(response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Aggregations