Search in sources :

Example 1 with FormEncodingBuilder

use of com.squareup.okhttp.FormEncodingBuilder in project SimpleNews by liuling07.

the class OkHttpUtils method buildPostRequest.

private Request buildPostRequest(String url, List<Param> params) {
    FormEncodingBuilder builder = new FormEncodingBuilder();
    for (Param param : params) {
        builder.add(param.key, param.value);
    }
    RequestBody requestBody = builder.build();
    return new Request.Builder().url(url).post(requestBody).build();
}
Also used : Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) RequestBody(com.squareup.okhttp.RequestBody)

Example 2 with FormEncodingBuilder

use of com.squareup.okhttp.FormEncodingBuilder in project remusic by aa112901.

the class HttpUtil method postUrl.

public static void postUrl(Context context, String j) {
    try {
        String action = "https://music.163.com/weapi/login/";
        RequestBody formBody = new FormEncodingBuilder().build();
        Log.e("post", "p");
        Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(formBody).build();
        mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.e("respose", response.body().string());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) CookieManager(java.net.CookieManager) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestBody(com.squareup.okhttp.RequestBody)

Example 3 with FormEncodingBuilder

use of com.squareup.okhttp.FormEncodingBuilder in project remusic by aa112901.

the class HttpUtil method postNetease.

public static void postNetease(Context context, String j) {
    try {
        String action = "https://music.163.com/weapi/login/";
        RequestBody formBody = new FormEncodingBuilder().add("params", "9NdyZTlp0Q/f1E1ora4tGM0uLYXqh7MD0mk7632ilWQvRDPZ02UkHrGFUccwW4HZYpacpPnmE+oMr/HI/vhuQvg8zYKgDP6NOaXG8nKDJpQTfOAiXT5KDrJOvb7ejSj/").add("encSeckey", "ae878167c394a959699c025a5c36043d0ae043c42d7f55fe4d1191c8ac9f3abe285b78c4a25ed6d9394a0ba0cb83a9a62de697199bd337f1de183bb07d6764a051495ea873ad615bb0a7e69f44d9168fc78ed1d61feb142ad06679dce58257ee9005756a18032ff499a4e24f7658bb59de2219f21f568301d43dba500e0c2d3b").build();
        String json = "{\"params\": \"9NdyZTlp0Q/f1E1ora4tGM0uLYXqh7MD0mk7632ilWQvRDPZ02UkHrGFUccwW4HZYpacpPnmE+oMr/HI/vhuQvg8zYKgDP6NOaXG8nKDJpQTfOAiXT5KDrJOvb7ejSj/\",  " + "\"encSecKey\": \"ae878167c394a959699c025a5c36043d0ae043c42d7f55fe4d1191c8ac9f3abe285b78c4a25ed6d9394a0ba0cb83a9a62de697199bd337f1de183bb07d6764a051495ea873ad615bb0a7e69f44d9168fc78ed1d61feb142ad06679dce58257ee9005756a18032ff499a4e24f7658bb59de2219f21f568301d43dba500e0c2d3b\"}";
        RequestBody requestBody = RequestBody.create(MediaType.parse("JSON"), json);
        Log.e("post", "p");
        Request request = new Request.Builder().url(action).header("Content-Type", "application/x-www-form-urlencoded").header("Host", "music.163.com").header("Cookie", "appver=1.5.0.75771").header("Referer", "http://music.163.com/").header("Connection", "keep-alive").header("Accept-Encoding", "gzip,deflate").header("Accept", "*/*").header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36").post(requestBody).build();
        mOkHttpClient.setCookieHandler(new CookieManager(new PersistentCookieStore(context.getApplicationContext()), CookiePolicy.ACCEPT_ALL));
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            Log.e("respose", response.body().string());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.squareup.okhttp.Response) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) CookieManager(java.net.CookieManager) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestBody(com.squareup.okhttp.RequestBody)

Example 4 with FormEncodingBuilder

use of com.squareup.okhttp.FormEncodingBuilder in project QuickAndroid by ImKarl.

the class OkHttp method createRequest.

private <T> Request createRequest(QAHttpMethod method, String url, QARequestParams params, final QAHttpCallback<T> listener) {
    if (method == null) {
        method = QAHttpMethod.GET;
    }
    final String finalUrl = url;
    if (method == QAHttpMethod.GET) {
        url = parseGetUrl(url, params != null ? params.getParams() : null);
        params = null;
    } else {
        if (params == null) {
            params = new QARequestParams();
        }
    }
    Request.Builder builder = new Request.Builder();
    try {
        builder.url(url);
    } catch (final Exception e) {
        sendFailedCallback(finalUrl, e, listener);
        return null;
    }
    // 强制使用缓存
    builder.cacheControl(CacheControl.FORCE_CACHE);
    // header
    Headers.Builder headerBuilder = new Headers.Builder();
    if (params != null && !params.getHeaders().isEmpty()) {
        for (Entry<String, String> entry : params.getHeaders().entrySet()) {
            headerBuilder.add(entry.getKey(), entry.getValue());
        }
    }
    builder.headers(headerBuilder.build());
    // param
    RequestBody requestBody = null;
    if (params != null) {
        if (!TextUtils.isEmpty(params.getBody())) {
            requestBody = RequestBody.create(MEDIA_TYPE_TEXT, params.getBody());
        } else {
            try {
                MultipartBuilder paramBuilder = new MultipartBuilder();
                if (params != null && !params.getParams().isEmpty()) {
                    for (Entry<String, List<Part>> entry : params.getParams().entrySet()) {
                        String name = entry.getKey();
                        List<Part> parts = entry.getValue();
                        if (parts != null && !parts.isEmpty()) {
                            for (Part part : parts) {
                                paramBuilder.addPart(part.header(), part.body());
                            }
                        }
                    }
                }
                requestBody = paramBuilder.build();
            } catch (IllegalStateException e) {
                requestBody = new FormEncodingBuilder().build();
            }
        }
    }
    builder.method(method.name(), requestBody == null ? null : new ProgressRequestBody(requestBody, new OnProgressListener() {

        @Override
        public void onProgress(long currentBytes, long contentLength) {
            // 上传进度
            sendProgressCallback(finalUrl, currentBytes, contentLength, QAHttpAction.REQUEST, listener);
        }
    }));
    final Request request = builder.build();
    mOnProgressListeners.put(request, new OnProgressListener() {

        @Override
        public void onProgress(long currentBytes, long contentLength) {
            // 下载进度
            sendProgressCallback(finalUrl, currentBytes, contentLength, QAHttpAction.RESPONSE, listener);
        }
    });
    return request;
}
Also used : Headers(com.squareup.okhttp.Headers) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) QANullException(cn.jeesoft.qa.error.QANullException) QANoSupportException(cn.jeesoft.qa.error.QANoSupportException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) QAException(cn.jeesoft.qa.error.QAException) IOException(java.io.IOException) StringPart(cn.jeesoft.qa.libcore.http.part.StringPart) Part(cn.jeesoft.qa.libcore.http.part.Part) List(java.util.List) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) QARequestParams(cn.jeesoft.qa.libcore.http.QARequestParams) RequestBody(com.squareup.okhttp.RequestBody)

Example 5 with FormEncodingBuilder

use of com.squareup.okhttp.FormEncodingBuilder in project LookLook by xinghongfei.

the class OkHttpUtils method buildPostRequest.

private Request buildPostRequest(String url, List<Param> params) {
    FormEncodingBuilder builder = new FormEncodingBuilder();
    for (Param param : params) {
        builder.add(param.key, param.value);
    }
    RequestBody requestBody = builder.build();
    return new Request.Builder().url(url).post(requestBody).build();
}
Also used : Request(com.squareup.okhttp.Request) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)5 Request (com.squareup.okhttp.Request)5 RequestBody (com.squareup.okhttp.RequestBody)5 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Response (com.squareup.okhttp.Response)2 CookieManager (java.net.CookieManager)2 QAException (cn.jeesoft.qa.error.QAException)1 QANoSupportException (cn.jeesoft.qa.error.QANoSupportException)1 QANullException (cn.jeesoft.qa.error.QANullException)1 QARequestParams (cn.jeesoft.qa.libcore.http.QARequestParams)1 Part (cn.jeesoft.qa.libcore.http.part.Part)1 StringPart (cn.jeesoft.qa.libcore.http.part.StringPart)1 Headers (com.squareup.okhttp.Headers)1 MultipartBuilder (com.squareup.okhttp.MultipartBuilder)1 List (java.util.List)1