Search in sources :

Example 56 with Request

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

the class OkHttpUtils method postRequest.

private void postRequest(String url, final ResultCallback callback, List<Param> params) {
    Request request = buildPostRequest(url, params);
    deliveryResult(callback, request);
}
Also used : Request(com.squareup.okhttp.Request)

Example 57 with Request

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

the class OkHttpUtils method deliveryResult.

private void deliveryResult(final ResultCallback callback, Request request) {
    mOkHttpClient.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Request request, final IOException e) {
            sendFailCallback(callback, e);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            try {
                String str = response.body().string();
                if (callback.mType == String.class) {
                    sendSuccessCallBack(callback, str);
                } else {
                    Object object = JsonUtils.deserialize(str, callback.mType);
                    sendSuccessCallBack(callback, object);
                }
            } catch (final Exception e) {
                LogUtils.e(TAG, "convert json failure", e);
                sendFailCallback(callback, e);
            }
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) Callback(com.squareup.okhttp.Callback) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException)

Example 58 with Request

use of com.squareup.okhttp.Request in project storymaker by StoryMaker.

the class StorymakerDownloadManager method downloadWithManager.

private void downloadWithManager(Uri uri, String title, String desc, Uri uriFile) {
    initDownloadManager();
    Timber.d("QUEUEING DOWNLOAD: " + uri.toString() + " -> " + uriFile.toString());
    initReceivers();
    DownloadManager.Request request = new DownloadManager.Request(uri).setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE).setAllowedOverRoaming(false).setTitle(title).setDescription(desc).setVisibleInDownloadsUi(false).setDestinationUri(uriFile);
    File partFile = new File(uriFile.toString().replace(".tmp", ".part"));
    if (partFile.exists()) {
        long partBytes = partFile.length();
        Timber.d("PARTIAL FILE " + partFile.getPath() + " FOUND, SETTING RANGE HEADER: " + "Range" + " / " + "bytes=" + Long.toString(partBytes) + "-");
        request.addRequestHeader("Range", "bytes=" + Long.toString(partBytes) + "-");
    } else {
        Timber.d("PARTIAL FILE " + partFile.getPath() + " NOT FOUND, STARTING AT BYTE 0");
    }
    lastDownload = dManager.enqueue(request);
    // have to enqueue first to get manager id
    String uriString = uriFile.toString();
    StorymakerQueueManager.addToQueue(context, Long.valueOf(lastDownload), uriString.substring(uriString.lastIndexOf("/") + 1), queueDao);
}
Also used : Request(com.squareup.okhttp.Request) DownloadManager(android.app.DownloadManager) File(java.io.File)

Example 59 with Request

use of com.squareup.okhttp.Request 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 60 with Request

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

Aggregations

Request (com.squareup.okhttp.Request)73 Response (com.squareup.okhttp.Response)47 IOException (java.io.IOException)41 OkHttpClient (com.squareup.okhttp.OkHttpClient)22 RequestBody (com.squareup.okhttp.RequestBody)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)11 File (java.io.File)10 Callback (com.squareup.okhttp.Callback)9 InputStream (java.io.InputStream)6 Gson (com.google.gson.Gson)4 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 MediaType (com.squareup.okhttp.MediaType)4 ResponseBody (com.squareup.okhttp.ResponseBody)4 FileOutputStream (java.io.FileOutputStream)4 Intent (android.content.Intent)3 SharedPreferences (android.content.SharedPreferences)3 Uri (android.net.Uri)3 Cache (com.squareup.okhttp.Cache)3 Call (com.squareup.okhttp.Call)3