Search in sources :

Example 1 with ProgressRequestBody

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;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) Uri(android.net.Uri) IOException(java.io.IOException) Response(okhttp3.Response) JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) File(java.io.File) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody) RequestBody(okhttp3.RequestBody) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody)

Example 2 with ProgressRequestBody

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;
}
Also used : Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.JSONObject) MultipartBody(okhttp3.MultipartBody) Request(okhttp3.Request) IOException(java.io.IOException) File(java.io.File) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody) ProgressRequestBody(me.ccrama.redditslide.util.ProgressRequestBody)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 ProgressRequestBody (me.ccrama.redditslide.util.ProgressRequestBody)2 MultipartBody (okhttp3.MultipartBody)2 OkHttpClient (okhttp3.OkHttpClient)2 Request (okhttp3.Request)2 RequestBody (okhttp3.RequestBody)2 Response (okhttp3.Response)2 JSONObject (org.json.JSONObject)2 Uri (android.net.Uri)1 MediaType (okhttp3.MediaType)1 BufferedSink (okio.BufferedSink)1