Search in sources :

Example 21 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project Infinity-For-Reddit by Docile-Alligator.

the class UploadImageUtils method parseJSONResponseFromAWS.

public static Map<String, RequestBody> parseJSONResponseFromAWS(String response) throws JSONException {
    JSONObject responseObject = new JSONObject(response);
    JSONArray nameValuePairs = responseObject.getJSONObject(JSONUtils.ARGS_KEY).getJSONArray(JSONUtils.FIELDS_KEY);
    Map<String, RequestBody> nameValuePairsMap = new HashMap<>();
    for (int i = 0; i < nameValuePairs.length(); i++) {
        nameValuePairsMap.put(nameValuePairs.getJSONObject(i).getString(JSONUtils.NAME_KEY), APIUtils.getRequestBody(nameValuePairs.getJSONObject(i).getString(JSONUtils.VALUE_KEY)));
    }
    return nameValuePairsMap;
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) RequestBody(okhttp3.RequestBody)

Example 22 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project Infinity-For-Reddit by Docile-Alligator.

the class SubmitPost method submitVideoPost.

public static void submitVideoPost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit, Retrofit uploadVideoRetrofit, String accessToken, String subredditName, String title, File buffer, String mimeType, Bitmap posterBitmap, Flair flair, boolean isSpoiler, boolean isNSFW, boolean receivePostReplyNotifications, SubmitPostListener submitPostListener) {
    RedditAPI api = oauthRetrofit.create(RedditAPI.class);
    String fileType = mimeType.substring(mimeType.indexOf("/") + 1);
    Map<String, String> uploadImageParams = new HashMap<>();
    uploadImageParams.put(APIUtils.FILEPATH_KEY, "post_video." + fileType);
    uploadImageParams.put(APIUtils.MIMETYPE_KEY, mimeType);
    Call<String> uploadImageCall = api.uploadImage(APIUtils.getOAuthHeader(accessToken), uploadImageParams);
    try {
        Response<String> uploadImageResponse = uploadImageCall.execute();
        if (uploadImageResponse.isSuccessful()) {
            Map<String, RequestBody> nameValuePairsMap = UploadImageUtils.parseJSONResponseFromAWS(uploadImageResponse.body());
            RequestBody fileBody = RequestBody.create(buffer, MediaType.parse("application/octet-stream"));
            MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", "post_video." + fileType, fileBody);
            RedditAPI uploadVideoToAWSApi;
            if (fileType.equals("gif")) {
                uploadVideoToAWSApi = uploadMediaRetrofit.create(RedditAPI.class);
            } else {
                uploadVideoToAWSApi = uploadVideoRetrofit.create(RedditAPI.class);
            }
            Call<String> uploadMediaToAWS = uploadVideoToAWSApi.uploadMediaToAWS(nameValuePairsMap, fileToUpload);
            Response<String> uploadMediaToAWSResponse = uploadMediaToAWS.execute();
            if (uploadMediaToAWSResponse.isSuccessful()) {
                String url = UploadImageUtils.parseXMLResponseFromAWS(uploadMediaToAWSResponse.body());
                if (url == null) {
                    submitPostListener.submitFailed(null);
                    return;
                }
                String imageUrlOrError = UploadImageUtils.uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, posterBitmap);
                if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
                    if (fileType.equals("gif")) {
                        submitPost(executor, handler, oauthRetrofit, accessToken, subredditName, title, url, flair, isSpoiler, isNSFW, receivePostReplyNotifications, APIUtils.KIND_VIDEOGIF, imageUrlOrError, submitPostListener);
                    } else {
                        submitPost(executor, handler, oauthRetrofit, accessToken, subredditName, title, url, flair, isSpoiler, isNSFW, receivePostReplyNotifications, APIUtils.KIND_VIDEO, imageUrlOrError, submitPostListener);
                    }
                } else {
                    submitPostListener.submitFailed(imageUrlOrError);
                }
            } else {
                submitPostListener.submitFailed(uploadMediaToAWSResponse.code() + " " + uploadMediaToAWSResponse.message());
            }
        } else {
            submitPostListener.submitFailed(uploadImageResponse.code() + " " + uploadImageResponse.message());
        }
    } catch (IOException | XmlPullParserException | JSONException e) {
        e.printStackTrace();
        submitPostListener.submitFailed(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) JSONException(org.json.JSONException) IOException(java.io.IOException) RedditAPI(ml.docilealligator.infinityforreddit.apis.RedditAPI) MultipartBody(okhttp3.MultipartBody) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) RequestBody(okhttp3.RequestBody)

Example 23 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project captcha by anji-plus.

the class BlockPuzzleDialog method loadCaptcha.

private void loadCaptcha() {
    Map<String, Object> params = new HashMap<>();
    params.put("captchaType", "blockPuzzle");
    JSONObject jsonObject = new JSONObject(params);
    RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
    RetrofitUtils.getServerApi().getAsync(body).compose(RxHelper.observableIO2Main(mContext)).subscribe(new BaseObserver<CaptchaGetIt>(mContext, true) {

        @Override
        public void onSuccess(CaptchaGetIt data) {
            baseImageBase64 = data.getOriginalImageBase64();
            slideImageBase64 = data.getJigsawImageBase64();
            token = data.getToken();
            key = data.getSecretKey();
            dragView.setUp(ImageUtil.base64ToBitmap(baseImageBase64), ImageUtil.base64ToBitmap(slideImageBase64));
            dragView.setSBUnMove(true);
            initEvent();
        }

        @Override
        public void onFailure(Throwable e, String errorMsg) {
            dragView.setSBUnMove(false);
            Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : CaptchaGetIt(com.example.verificationcodejavademo.model.CaptchaGetIt) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.json.JSONObject) RequestBody(okhttp3.RequestBody)

Example 24 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project captcha by anji-plus.

the class WordCaptchaDialog method loadCaptcha.

private void loadCaptcha() {
    bottomTitle.setText("数据加载中......");
    bottomTitle.setTextColor(Color.BLACK);
    Map<String, Object> params = new HashMap<>();
    params.put("captchaType", "clickWord");
    JSONObject jsonObject = new JSONObject(params);
    RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
    Log.i("wuyan", "body:" + jsonObject);
    RetrofitUtils.getServerApi().getWordCaptchaAsync(body).compose(RxHelper.observableIO2Main(mContext)).subscribe(new BaseObserver<WordCaptchaGetIt>(mContext, true) {

        @Override
        public void onSuccess(WordCaptchaGetIt data) {
            baseImageBase64 = data.getOriginalImageBase64();
            token = data.getToken();
            key = data.getSecretKey();
            String wordStr = "";
            int j = 0;
            for (int i = 0; i < data.getWordList().size(); i++) {
                j++;
                wordStr += data.getWordList().get(i);
                if (j < data.getWordList().size()) {
                    wordStr += ",";
                }
            }
            wordView.setSize(data.getWordList().size());
            bottomTitle.setText("请依此点击【" + wordStr + "】");
            bottomTitle.setTextColor(Color.BLACK);
            wordView.setUp(ImageUtil.base64ToBitmap(baseImageBase64));
            initEvent();
        }

        @Override
        public void onFailure(Throwable e, String errorMsg) {
            bottomTitle.setText("加载失败,请刷新");
            bottomTitle.setTextColor(Color.RED);
            wordView.setSize(-1);
            Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.json.JSONObject) WordCaptchaGetIt(com.example.verificationcodejavademo.model.WordCaptchaGetIt) RequestBody(okhttp3.RequestBody)

Example 25 with RequestBody

use of com.reprezen.kaizen.oasparser.model3.RequestBody in project captcha by anji-plus.

the class WordCaptchaDialog method checkCaptcha.

private void checkCaptcha(String cryptedStr) {
    Map<String, Object> params = new HashMap<>();
    params.put("captchaType", "clickWord");
    params.put("token", token);
    params.put("pointJson", AESUtil.encode(cryptedStr, key));
    JSONObject jsonObject = new JSONObject(params);
    RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
    Log.i("wuyan", "body:" + jsonObject);
    RetrofitUtils.getServerApi().checkAsync(body).compose(RxHelper.observableIO2Main(mContext)).subscribe(new BaseObserver<CaptchaCheckIt>(mContext, false) {

        @Override
        public void onSuccess(CaptchaCheckIt data) {
            bottomTitle.setText("验证成功");
            bottomTitle.setTextColor(Color.GREEN);
            wordView.ok();
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    loadCaptcha();
                    dismiss();
                }
            }, 1000);
            if (mOnResultsListener != null) {
                String result = token + "---" + cryptedStr;
                mOnResultsListener.onResultsClick(AESUtil.encode(result, key));
            }
        }

        @Override
        public void onFailure(Throwable e, String errorMsg) {
            bottomTitle.setText("验证失败");
            bottomTitle.setTextColor(Color.RED);
            wordView.fail();
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // 刷新验证码
                    loadCaptcha();
                }
            }, 1000);
        }
    });
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONObject(org.json.JSONObject) CaptchaCheckIt(com.example.verificationcodejavademo.model.CaptchaCheckIt) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)1358 Request (okhttp3.Request)785 Response (okhttp3.Response)598 IOException (java.io.IOException)420 Test (org.junit.Test)235 OkHttpClient (okhttp3.OkHttpClient)216 MultipartBody (okhttp3.MultipartBody)213 MediaType (okhttp3.MediaType)204 Call (okhttp3.Call)198 JSONObject (org.json.JSONObject)183 ResponseBody (okhttp3.ResponseBody)177 Callback (okhttp3.Callback)115 FormBody (okhttp3.FormBody)106 Buffer (okio.Buffer)98 File (java.io.File)92 Map (java.util.Map)90 JsonObject (io.vertx.core.json.JsonObject)89 Headers (okhttp3.Headers)88 HashMap (java.util.HashMap)83 HttpUrl (okhttp3.HttpUrl)80