Search in sources :

Example 11 with RequestBody

use of com.squareup.okhttp.RequestBody in project PocketHub by pockethub.

the class ImageBinPoster method post.

/**
     * Post the image to ImageBin
     *
     * @param bytes Bytes of the image to post
     * @param callback Request callback
     */
public static void post(byte[] bytes, Callback callback) {
    RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart("file", "test", RequestBody.create(MediaType.parse("image/*"), bytes)).build();
    Request request = new Request.Builder().url("https://imagebin.ca/upload.php").post(requestBody).build();
    OkHttpClient client = new OkHttpClient();
    Call call = client.newCall(request);
    call.enqueue(callback);
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) MultipartBuilder(com.squareup.okhttp.MultipartBuilder) RequestBody(com.squareup.okhttp.RequestBody)

Example 12 with RequestBody

use of com.squareup.okhttp.RequestBody 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 13 with RequestBody

use of com.squareup.okhttp.RequestBody 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 14 with RequestBody

use of com.squareup.okhttp.RequestBody in project ButterRemote-Android by se-bastiaan.

the class PopcornTimeRpcClient method request.

/**
     * Send JSON RPC request to the instance
     * @param rpc Request data
     * @param callback Callback for the request
     * @return ResponseFuture
     */
private Call request(final RpcRequest rpc, final Callback callback) {
    RequestBody requestBody = RequestBody.create(MEDIA_TYPE_JSON, mGson.toJson(rpc));
    Request request = new Request.Builder().url(mUrl).header("Authorization", Credentials.basic(mUsername, mPassword)).post(requestBody).build();
    Call call = mClient.newCall(request);
    call.enqueue(new com.squareup.okhttp.Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            callback.onCompleted(e, null);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            RpcResponse result = null;
            Exception e = null;
            try {
                if (response != null && response.isSuccessful()) {
                    String responseStr = response.body().string();
                    //LogUtils.d("PopcornTimeRpcClient", "Response: " + responseStr);
                    result = mGson.fromJson(responseStr, RpcResponse.class);
                    LinkedTreeMap<String, Object> map = result.getMapResult();
                    if (map.containsKey("popcornVersion")) {
                        mVersion = (String) map.get("popcornVersion");
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                e = ex;
                mVersion = ZERO_VERSION;
                if (rpc.id == RequestId.GET_SELECTION.ordinal()) {
                    mVersion = "0.3.4";
                }
            }
            callback.onCompleted(e, result);
        }
    });
    return call;
}
Also used : Call(com.squareup.okhttp.Call) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Request(com.squareup.okhttp.Request) Callback(com.squareup.okhttp.Callback) IOException(java.io.IOException) IOException(java.io.IOException) Response(com.squareup.okhttp.Response) RequestBody(com.squareup.okhttp.RequestBody)

Example 15 with RequestBody

use of com.squareup.okhttp.RequestBody 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)

Aggregations

RequestBody (com.squareup.okhttp.RequestBody)20 Request (com.squareup.okhttp.Request)19 Response (com.squareup.okhttp.Response)13 IOException (java.io.IOException)13 OkHttpClient (com.squareup.okhttp.OkHttpClient)8 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 MediaType (com.squareup.okhttp.MediaType)3 MultipartBuilder (com.squareup.okhttp.MultipartBuilder)3 Intent (android.content.Intent)2 Call (com.squareup.okhttp.Call)2 CookieManager (java.net.CookieManager)2 BufferedSink (okio.BufferedSink)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 AccountAuthenticatorResponse (android.accounts.AccountAuthenticatorResponse)1 AccountManager (android.accounts.AccountManager)1 SharedPreferences (android.content.SharedPreferences)1 Bundle (android.os.Bundle)1 QAException (cn.jeesoft.qa.error.QAException)1