Search in sources :

Example 6 with Callback

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

the class EditIssueActivity method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_SELECT_PHOTO && resultCode == Activity.RESULT_OK) {
        progressDialog = new MaterialDialog.Builder(this).content(R.string.loading).progress(true, 0).build();
        progressDialog.show();
        ImageBinPoster.post(this, data.getData(), new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                progressDialog.dismiss();
                showImageError();
            }

            @Override
            public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                progressDialog.dismiss();
                if (response.isSuccessful()) {
                    insertImage(ImageBinPoster.getUrl(response.body().string()));
                } else {
                    showImageError();
                }
            }
        });
    }
}
Also used : Callback(com.squareup.okhttp.Callback) Builder(com.github.pockethub.android.Intents.Builder) Request(com.squareup.okhttp.Request) IssueRequest(com.meisolsson.githubsdk.model.request.issue.IssueRequest) IOException(java.io.IOException)

Example 7 with Callback

use of com.squareup.okhttp.Callback in project nmid-headline by miao1007.

the class WebViewFragment method trySetupWebview.

private void trySetupWebview() {
    // http://202.202.43.205:8086/api/android/newscontent?category=1&id=194
    url = HeadlineService.END_POINT + "/api/android/newscontent?id=" + feed.getIdmember() + "&category=" + feed.getCategory();
    WebSettings settings = mWebView.getSettings();
    mWebView.setWebContentsDebuggingEnabled(true);
    settings.setTextZoom(WebViewPref.getWebViewTextZoom(getActivity()));
    switch(WebViewPref.isAutoLoadImages(getActivity())) {
        case 0:
            settings.setLoadsImagesAutomatically(false);
            break;
        case 1:
            break;
        case 2:
            if (!NetworkUtils.isWifiAviliable(getActivity())) {
                settings.setLoadsImagesAutomatically(false);
            }
            break;
    }
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
        }

        @Override
        public void onResponse(Response response) throws IOException {
            String htmlData;
            if (ThemePref.isNightMode(getActivity())) {
                // Webview will use asserts/style_night.css
                htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style_night.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
            } else {
                // Webview will use asserts/style.css
                htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" /> <body class= \"gloable\"> " + response.body().string() + "</body>";
            }
            Log.d(TAG, Thread.currentThread().getName());
            getActivity().runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mWebView.loadDataWithBaseURL("file:///android_asset/", htmlData, MIME_TYPE, ENCODING, null);
                }
            });
        }
    });
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Callback(com.squareup.okhttp.Callback) WebSettings(android.webkit.WebSettings) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 8 with Callback

use of com.squareup.okhttp.Callback in project actor-platform by actorapp.

the class AndroidHttpProvider method getMethod.

@Override
public Promise<HTTPResponse> getMethod(String url, int startOffset, int size, int totalSize) {
    return new Promise<>(resolver -> {
        final Request request = new Request.Builder().url(url).addHeader("Range", "bytes=" + startOffset + "-" + (startOffset + size)).build();
        Log.d(TAG, "Downloading part: " + request.toString());
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
                Log.d(TAG, "Downloading part error: " + request.toString());
                e.printStackTrace();
                // TODO: Better error?
                resolver.error(new HTTPError(0));
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d(TAG, "Downloading part response: " + request.toString() + " -> " + response.toString());
                if (response.code() >= 200 && response.code() < 300) {
                    resolver.result(new HTTPResponse(response.code(), response.body().bytes()));
                } else {
                    resolver.error(new HTTPError(response.code()));
                }
            }
        });
    });
}
Also used : Response(com.squareup.okhttp.Response) HTTPResponse(im.actor.runtime.http.HTTPResponse) Promise(im.actor.runtime.promise.Promise) HTTPError(im.actor.runtime.http.HTTPError) Callback(com.squareup.okhttp.Callback) HTTPResponse(im.actor.runtime.http.HTTPResponse) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Example 9 with Callback

use of com.squareup.okhttp.Callback 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 10 with Callback

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

Aggregations

Callback (com.squareup.okhttp.Callback)10 Request (com.squareup.okhttp.Request)10 IOException (java.io.IOException)10 Response (com.squareup.okhttp.Response)9 OkHttpClient (com.squareup.okhttp.OkHttpClient)2 HTTPError (im.actor.runtime.http.HTTPError)2 HTTPResponse (im.actor.runtime.http.HTTPResponse)2 Promise (im.actor.runtime.promise.Promise)2 Handler (android.os.Handler)1 NonNull (android.support.annotation.NonNull)1 WebSettings (android.webkit.WebSettings)1 QAHttpCallback (cn.jeesoft.qa.libcore.http.QAHttpCallback)1 Builder (com.github.pockethub.android.Intents.Builder)1 LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)1 IssueRequest (com.meisolsson.githubsdk.model.request.issue.IssueRequest)1 Cell (com.secupwn.aimsicd.utils.Cell)1 Call (com.squareup.okhttp.Call)1 RequestBody (com.squareup.okhttp.RequestBody)1