Example usage for com.squareup.okhttp Callback Callback

List of usage examples for com.squareup.okhttp Callback Callback

Introduction

In this page you can find the example usage for com.squareup.okhttp Callback Callback.

Prototype

Callback

Source Link

Usage

From source file:com.librelio.activity.StartupActivity.java

License:Apache License

private void loadAdvertisingImage() {

    client.setReadTimeout(2, TimeUnit.SECONDS);
    Request request = new Request.Builder().url(getAdvertisingImageURL()).build();

    client.newCall(request).enqueue(new Callback() {

        @Override/* www . j  a  v  a  2 s .c  o  m*/
        public void onResponse(Response response) throws IOException {
            if (response.code() == 200) {
                EasyTracker.getTracker()
                        .sendView("Interstitial/" + FilenameUtils.getName(getAdvertisingImageURL()));
                byte[] bytes = response.body().bytes();
                if (bytes != null) {
                    adImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                }
                loadAdvertisingLinkAndDisplayAdvertising();
            } else {
                showMagazineAfterDelay(DEFAULT_ADV_DELAY);
            }
            response.body().close();
        }

        @Override
        public void onFailure(Request request, Throwable throwable) {
            showMagazineAfterDelay(DEFAULT_ADV_DELAY);
        }
    });
}

From source file:com.librelio.activity.StartupActivity.java

License:Apache License

private void loadAdvertisingLinkAndDisplayAdvertising() {

    Request request = new Request.Builder().url(getAdvertisingLinkURL()).build();

    client.newCall(request).enqueue(new Callback() {

        @Override/* w w w . j a  v  a  2  s  .c om*/
        public void onResponse(Response response) throws IOException {
            if (response.code() == 200) {
                PListXMLHandler handler = new PListXMLHandler();
                PListXMLParser parser = new PListXMLParser();
                parser.setHandler(handler);
                parser.parse(response.body().string());
                PList list = ((PListXMLHandler) parser.getHandler()).getPlist();
                if (list != null) {
                    Dict dict = (Dict) list.getRootElement();
                    String delay = dict.getString(PLIST_DELAY).getValue().toString();
                    String link = dict.getString(PLIST_LINK).getValue().toString();
                    if (adImage != null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                advertisingImage.setImageBitmap(adImage);
                                if (isFirstImage) {
                                    applyRotation(0, 90);
                                    isFirstImage = !isFirstImage;
                                } else {
                                    applyRotation(0, -90);
                                    isFirstImage = !isFirstImage;
                                }

                            }
                        });
                    }
                    setOnAdvertisingImageClickListener(link);
                    showMagazineAfterDelay(Integer.valueOf(delay));
                }
            } else {
                showMagazineAfterDelay(DEFAULT_ADV_DELAY);
            }
            response.body().close();
        }

        @Override
        public void onFailure(Request request, Throwable throwable) {
            showMagazineAfterDelay(DEFAULT_ADV_DELAY);
        }
    });
}

From source file:com.lidroid.xutils.HttpUtils.java

License:Apache License

public <T> void sendAsync(HttpRequest.HttpMethod method, String url, RequestParams params,
        final RequestCallBack<T> callBack) {

    Request.Builder builder = buildRequest(method, url, params);

    mOkHttpClient.newCall(builder.build()).enqueue(new Callback() {
        @Override/*w w  w  . j  ava2  s.  c  om*/
        public void onFailure(Request request, IOException e) {
            callBack.onFailure(new HttpException(), e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response != null && response.body() != null) {
                callBack.onSuccess(new ResponseInfo<T>(null, (T) response.body().string(), false));
            } else {
                callBack.onFailure(new HttpException("Response is null"), "Response is null");
            }
        }
    });
}

From source file:com.liferay.mobile.android.auth.CookieSignIn.java

License:Open Source License

protected static Callback getCallback(final String server, final CookieCallback callback,
        final CookieManager cookieManager, final CookieAuthentication authentication) {

    return new Callback() {

        @Override//from  w w w. ja  va2  s. co  m
        public void onFailure(Request request, IOException ioe) {
            callback.onFailure(ioe);
        }

        @Override
        public void onResponse(Response response) {
            try {
                Session session = parseResponse(response, server, cookieManager, authentication);

                callback.onSuccess(session);
            } catch (Exception e) {
                callback.onFailure(e);
            }
        }
    };
}

From source file:com.liferay.mobile.sdk.auth.CookieSignIn.java

License:Open Source License

protected static Callback getCallback(final CookieCallback callback, final CookieManager cookieManager) {

    return new Callback() {

        @Override/*from  www .  ja  v  a2  s. c o  m*/
        public void onFailure(Request request, IOException ioe) {
            callback.onFailure(ioe);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            String body = response.body().string();

            Integer position = body.indexOf(AUTH_TOKEN) + AUTH_TOKEN.length();

            String authToken = body.substring(position, position + TOKEN_LENGTH);

            String cookieHeader = getHttpCookies(cookieManager.getCookieStore());

            if (Validator.isNotNull(cookieHeader)) {
                CookieAuthentication auth = new CookieAuthentication(authToken, cookieHeader);

                callback.onSuccess(auth);
            } else {
                callback.onFailure(new AuthenticationException("Cookie invalid or empty"));
            }
        }

    };
}

From source file:com.meyersj.tamalenow.location.UpdateLocationService.java

License:Open Source License

public void postLocation(Location location) {
    String date = Utils.dateFormat.format(new Date());
    Log.d(TAG, date);//  www . j  a  v a  2s .  co  m
    RequestBody formBody = new FormEncodingBuilder().add("vendor_id", "test_vendor")
            .add("tstamp", Utils.dateFormat.format(new Date()))
            .add("lat", String.valueOf(location.getLatitude()))
            .add("lon", String.valueOf(location.getLongitude())).build();

    Request request = new Request.Builder().url(app.getAPIBase() + "/" + Endpoints.LOCATION).post(formBody)
            .build();

    httpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            Log.d(TAG, "failure");
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (!response.isSuccessful())
                throw new IOException("Unexpected code " + response);
            Log.d(TAG, response.body().string());
        }
    });

}

From source file:com.monmonja.library.server.JsonRpcRequest.java

License:Open Source License

public void run(OkHttpClient client, final Success success, final Failure failure) {
    client.newCall(this.request).enqueue(new Callback() {
        @Override/*from w  ww .j a  v a  2s. c o m*/
        public void onFailure(Request request, IOException e) {
            failure.failure(e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            String responseData = response.body().string();
            JsonResponse jsonResponse = gson.fromJson(responseData, JsonResponse.class);
            if (jsonResponse.result != null) {
                success.success(gson.fromJson(jsonResponse.result, clazz));
            } else {
                Log.d("JsonRpc", responseData);
                failure.failure("JSONPRC2 error");
                //                    successHandler.sendEmptyMessage(0);
            }
        }
    });
}

From source file:com.mummyding.app.leisure.database.cache.cache.DailyCache.java

License:Open Source License

public void load() {
    Request.Builder builder = new Request.Builder();
    builder.url(DailyApi.daily_url);/*  ww  w. j  a  v a2s  .c om*/
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }
            String res = response.body().string();

            ArrayList<String> collectionTitles = new ArrayList<String>();
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).isCollected() == 1) {
                    collectionTitles.add(mList.get(i).getTitle());
                }
            }

            List<StoryBean> oldList = new ArrayList<StoryBean>();
            List<StoryBean> newList = new ArrayList<StoryBean>();

            for (StoryBean storyBean : mList) {
                oldList.add(storyBean);
            }

            Gson gson = new Gson();
            DailyBean dailyBean = gson.fromJson(res, DailyBean.class);
            StoryBean[] storyBeans = dailyBean.getStories();
            for (StoryBean storyBeen : storyBeans) {
                newList.add(storyBeen);
            }

            loadOld(dailyBean.getDate(), oldList, newList);

        }
    });
}

From source file:com.mummyding.app.leisure.database.cache.cache.DailyCache.java

License:Open Source License

private void loadOld(String date, final List<StoryBean> oldList, final List<StoryBean> newList) {
    Request.Builder builder = new Request.Builder();
    builder.url(DailyApi.daily_old_url + date);
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override/*w  w  w.  j  av a2  s  .  c  o m*/
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }
            String res = response.body().string();

            ArrayList<Integer> collectionIDs = new ArrayList<Integer>();
            for (int i = 0; i < oldList.size(); i++) {
                if (oldList.get(i).isCollected() == 1) {
                    collectionIDs.add(oldList.get(i).getId());
                }
            }

            // clear old data
            mList.clear();

            Gson gson = new Gson();
            StoryBean[] storyBeans = (gson.fromJson(res, DailyBean.class)).getStories();
            for (StoryBean storyBeen : storyBeans) {
                newList.add(storyBeen);
            }

            for (StoryBean storyBean : newList) {
                mList.add(storyBean);
            }

            // setCollection flag
            for (Integer id : collectionIDs) {
                for (int i = 0; i < mList.size(); i++) {
                    if (id.equals(mList.get(i).getId())) {
                        mList.get(i).setCollected(1);
                    }
                }
            }

            // notify
            mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
        }
    });
}

From source file:com.mummyding.app.leisure.database.cache.cache.NewsCache.java

License:Open Source License

@Override
public void load() {
    Request.Builder builder = new Request.Builder();
    builder.url(mUrl);/*from ww w.ja  va  2 s  . c o  m*/
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(com.squareup.okhttp.Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }
            InputStream is = new ByteArrayInputStream(
                    response.body().string().getBytes(Charset.forName("UTF-8")));
            try {
                ArrayList<String> collectionTitles = new ArrayList<String>();
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i).getIs_collected() == 1) {
                        collectionTitles.add(mList.get(i).getTitle());
                    }
                }

                mList.clear();
                mList.addAll(SAXNewsParse.parse(is));
                for (String title : collectionTitles) {
                    for (int i = 0; i < mList.size(); i++) {
                        if (title.equals(mList.get(i).getTitle())) {
                            mList.get(i).setIs_collected(1);
                        }
                    }
                }
                is.close();
                mHandler.sendEmptyMessage(CONSTANT.ID_SUCCESS);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

}