Search in sources :

Example 11 with FormBody

use of okhttp3.FormBody 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 12 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method search.

public static String search(String kw) {
    // &pageSize=20&keyword
    FormBody.Builder buider = new FormBody.Builder();
    buider.add("pageSize", "20").add("keyword", kw).add("page", "1");
    // 
    FormBody formBody = APIheper.getFormBuider(buider).build();
    // String url=host + "/api/recom/index";
    String url = host + "/api/book/search";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString();
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 13 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method getRecom.

public static String getRecom() {
    // &pageSize=20&keyword
    FormBody.Builder buider = new FormBody.Builder();
    // 
    FormBody formBody = APIheper.getFormBuider(buider).build();
    String url = host + "/api/recom/index";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString(4);
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 14 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method getRanking.

public static String getRanking(String type, int page, int pageSize) {
    // rankType=newOnline&page=1
    FormBody.Builder buider = new FormBody.Builder();
    buider.add("rankType", type);
    buider.add("page", page + "");
    buider.add("pageSize", pageSize + "");
    // 
    FormBody formBody = APIheper.getFormBuider(buider).build();
    String url = host + "/api/book/ranking";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            return new JSONObject(response.body().string()).toString(4);
        } else {
            return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        return "error:" + e.getMessage();
    }
}
Also used : Response(okhttp3.Response) JSONObject(org.json.JSONObject) FormBody(okhttp3.FormBody) Request(okhttp3.Request)

Example 15 with FormBody

use of okhttp3.FormBody in project Manhua by ag2s20150909.

the class API method search.

public static SearchBean search(String kw, int page, int pagesize) {
    Gson gson = new Gson();
    SearchBean result = new SearchBean();
    FormBody.Builder buider = new FormBody.Builder();
    buider.add("pageSize", pagesize + "").add("keyword", kw).add("page", page + "");
    FormBody formBody = APIheper.getFormBuider(buider).build();
    String url = host + "/api/book/search";
    Request request = new Request.Builder().post(formBody).url(url).build();
    try {
        Response response = APP.getOkhttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            result = gson.fromJson(response.body().string(), SearchBean.class);
            return result;
        } else {
            result.code = 1;
            result.success = false;
            result.message = "nessage:" + response.message() + " \ncode:" + response.code();
            return result;
        // return "error:" + response.message() + " errorcode:" + response.code();
        }
    } catch (Exception e) {
        result.code = 1;
        result.success = false;
        result.message = e.getLocalizedMessage();
        return result;
    }
}
Also used : Response(okhttp3.Response) SearchBean(cn.liuyin.manhua.data.bean.SearchBean) FormBody(okhttp3.FormBody) Request(okhttp3.Request) Gson(com.google.gson.Gson)

Aggregations

Request (okhttp3.Request)61 Response (okhttp3.Response)58 FormBody (okhttp3.FormBody)53 RequestBody (okhttp3.RequestBody)43 IOException (java.io.IOException)39 Call (okhttp3.Call)32 Callback (okhttp3.Callback)29 JSONObject (org.json.JSONObject)20 Map (java.util.Map)15 HttpUrl (okhttp3.HttpUrl)10 OkHttpClient (okhttp3.OkHttpClient)10 MultipartBody (okhttp3.MultipartBody)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)6 MediaType (okhttp3.MediaType)6 TypeToken (com.google.gson.reflect.TypeToken)4 File (java.io.File)3 DataInputStream (java.io.DataInputStream)2 List (java.util.List)2 ProgressRequestBody (me.ccrama.redditslide.util.ProgressRequestBody)2