Search in sources :

Example 1 with NetworkRequest

use of forpdateam.ru.forpda.api.NetworkRequest in project ForPDA by RadiationX.

the class Client method prepareRequest.

private Request.Builder prepareRequest(NetworkRequest request, ProgressListener uploadProgressListener) {
    String url = request.getUrl();
    if (request.getUrl().substring(0, 2).equals("//")) {
        url = "https:".concat(request.getUrl());
    }
    Log.d(LOG_TAG, "Request url " + request.getUrl());
    Request.Builder requestBuilder = new Request.Builder().url(url).header("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4").header("User-Agent", USER_AGENT);
    if (request.getHeaders() != null) {
        for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
            Log.d(LOG_TAG, "Header " + entry.getKey() + " : " + (privateHeaders.contains(entry.getKey()) ? "private" : entry.getValue()));
            requestBuilder.header(entry.getKey(), entry.getValue());
        }
    }
    if (request.getFormHeaders() != null || request.getFile() != null) {
        Log.d(LOG_TAG, "Multipart " + request.isMultipartForm());
        if (request.getFormHeaders() != null) {
            for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
                Log.d(LOG_TAG, "Form header " + entry.getKey() + " : " + (privateHeaders.contains(entry.getKey()) ? "private" : entry.getValue()));
            }
        }
        if (request.getFile() != null) {
            Log.d(LOG_TAG, "Form file " + request.getFile().toString());
        }
        if (!request.isMultipartForm()) {
            if (request.getFormHeaders() != null) {
                FormBody.Builder formBuilder = new FormBody.Builder();
                for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
                    formBuilder.add(entry.getKey(), entry.getValue());
                    if (request.getEncodedFormHeaders() != null && request.getEncodedFormHeaders().contains(entry.getKey())) {
                        formBuilder.addEncoded(entry.getKey(), entry.getValue());
                    } else {
                        formBuilder.add(entry.getKey(), entry.getValue());
                    }
                }
                FormBody formBody = formBuilder.build();
                requestBuilder.post(formBody);
            }
        } else {
            MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
            multipartBuilder.setType(MultipartBody.FORM);
            if (request.getFormHeaders() != null) {
                for (Map.Entry<String, String> entry : request.getFormHeaders().entrySet()) {
                    multipartBuilder.addFormDataPart(entry.getKey(), entry.getValue());
                }
            }
            if (request.getFile() != null) {
                MediaType type = MediaType.parse(request.getFile().getMimeType());
                RequestBody requestBody = RequestBodyUtil.create(type, request.getFile().getFileStream());
                multipartBuilder.addFormDataPart(request.getFile().getRequestName(), request.getFile().getFileName(), requestBody);
            }
            MultipartBody multipartBody = multipartBuilder.build();
            if (uploadProgressListener == null) {
                requestBuilder.post(multipartBody);
            } else {
                requestBuilder.post(new ProgressRequestBody(multipartBody, uploadProgressListener));
            }
        }
    }
    return requestBuilder;
}
Also used : NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) Request(okhttp3.Request) FormBody(okhttp3.FormBody) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(okhttp3.RequestBody)

Example 2 with NetworkRequest

use of forpdateam.ru.forpda.api.NetworkRequest in project ForPDA by RadiationX.

the class Client method request.

public NetworkResponse request(NetworkRequest request, OkHttpClient client, ProgressListener uploadProgressListener) throws Exception {
    Request.Builder requestBuilder = prepareRequest(request, uploadProgressListener);
    NetworkResponse response = new NetworkResponse(request.getUrl());
    Response okHttpResponse = null;
    try {
        okHttpResponse = client.newCall(requestBuilder.build()).execute();
        if (!okHttpResponse.isSuccessful()) {
            if (okHttpResponse.code() == 403) {
                String content = okHttpResponse.body().string();
                // forpdateam.ru.forpda.utils.ApiUtils.longLog(content);
                new Handler(Looper.getMainLooper()).post(() -> {
                    try {
                        if (TabManager.get().getTagContainClass(GoogleCaptchaFragment.class) == null) {
                            Bundle args = new Bundle();
                            args.putString("content", content);
                            TabManager.get().add(GoogleCaptchaFragment.class, args);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                });
            }
            throw new OkHttpResponseException(okHttpResponse.code(), okHttpResponse.message(), request.getUrl());
        }
        response.setCode(okHttpResponse.code());
        response.setMessage(okHttpResponse.message());
        response.setRedirect(okHttpResponse.request().url().toString());
        if (!request.isWithoutBody()) {
            response.setBody(okHttpResponse.body().string());
            getCounts(response.getBody());
            checkForumErrors(response.getBody());
        }
        Log.d(LOG_TAG, "Response: " + response.toString());
    } finally {
        if (okHttpResponse != null)
            okHttpResponse.close();
    }
    return response;
}
Also used : Response(okhttp3.Response) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) Bundle(android.os.Bundle) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) Request(okhttp3.Request) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse) Handler(android.os.Handler) GoogleCaptchaFragment(forpdateam.ru.forpda.ui.fragments.other.GoogleCaptchaFragment) GeneralSecurityException(java.security.GeneralSecurityException)

Example 3 with NetworkRequest

use of forpdateam.ru.forpda.api.NetworkRequest in project ForPDA by RadiationX.

the class Theme method reportPost.

public String reportPost(int topicId, int postId, String message) throws Exception {
    NetworkRequest request = new NetworkRequest.Builder().url("https://4pda.ru/forum/index.php?act=report&send=1&t=" + Integer.toString(topicId) + "&p=" + Integer.toString(postId)).formHeader("message", URLEncoder.encode(message, "windows-1251"), true).build();
    NetworkResponse response = Api.getWebClient().request(request);
    Pattern p = Pattern.compile("<div class=\"errorwrap\">\n" + "\\s*<h4>Причина:</h4>\n" + "\\s*\n" + "\\s*<p>(.*)</p>", Pattern.MULTILINE);
    Matcher m = p.matcher(response.getBody());
    return m.find() ? "Ошибка отправки жалобы: ".concat(m.group(1)) : "Жалоба отправлена";
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) NetworkRequest(forpdateam.ru.forpda.api.NetworkRequest) NetworkResponse(forpdateam.ru.forpda.api.NetworkResponse)

Aggregations

NetworkRequest (forpdateam.ru.forpda.api.NetworkRequest)3 NetworkResponse (forpdateam.ru.forpda.api.NetworkResponse)2 Request (okhttp3.Request)2 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 GoogleCaptchaFragment (forpdateam.ru.forpda.ui.fragments.other.GoogleCaptchaFragment)1 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 FormBody (okhttp3.FormBody)1 MediaType (okhttp3.MediaType)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1