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:cn.edu.zafu.news.ui.main.NewsFragment.java

License:Apache License

private synchronized void loadMore() {
    isLoadingMore = true;/*ww w  .ja  v  a  2 s.  c  om*/
    if (page >= max) {
        Toast.makeText(getActivity(), "?", Toast.LENGTH_LONG).show();
        return;
    }
    page++;
    OkHttpClient client = NewsOkHttpClient.getInstance();
    final Request request = new Request.Builder().url(category.getUrl() + page).build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            handler.sendEmptyMessage(LOADMORE_ERROR);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            NewsParser newsParser = new NewsParser();
            list.addAll(newsParser.convert(response.body().string()));
            handler.sendEmptyMessage(LOADMORE);
        }
    });
}

From source file:cn.gen.superwechat.activity.LoginActivity.java

License:Open Source License

private void loginSuccess() {
    try {/*  w  ww. ja  v  a  2s  .  co  m*/
        // ** ?logout???
        // ** manually load all local groups and
        EMGroupManager.getInstance().loadAllGroups();
        EMChatManager.getInstance().loadAllConversations();
        // ??
        initializeContacts();
        //?sd??
        //http://10.0.2.2:8080/SuperWeChatServer/Server?
        // request=download_avatar&avatarType=
        final OkHttpUtils<Message> utils = new OkHttpUtils<Message>();
        utils.url(SuperWeChatApplication.SERVER_ROOT)//??
                .addParam(I.KEY_REQUEST, I.REQUEST_DOWNLOAD_AVATAR)//?
                .addParam(I.AVATAR_TYPE, currentUsername)//?
                .doInBackground(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        //                    Toast.makeText(mContext,e.getMessage(),Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                        String avatarPath = I.AVATAR_TYPE_USER_PATH + I.BACKSLASH + currentUsername
                                + I.AVATAR_SUFFIX_JPG;
                        File file = OnSetAvatarListener.getAvatarFile(mContext, avatarPath);
                        FileOutputStream out = null;
                        out = new FileOutputStream(file);
                        utils.downloadFile(response, file, false);
                    }
                }).execute(null);
        //???
        Log.e("main", "start dowmload arraylist");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new DownloadContactList(mContext, currentUsername).execute();
                new DownloadAllGroupTask(mContext, currentUsername).execute();
                new DownloadPublicGroupTask(mContext, currentUsername, I.PAGE_ID_DEFAULT, I.PAGE_SIZE_DEFAULT)
                        .execute();
            }
        });
        //??
        initializeContacts();
    } catch (Exception e) {
        e.printStackTrace();
        // ?????
        runOnUiThread(new Runnable() {
            public void run() {
                pd.dismiss();
                DemoHXSDKHelper.getInstance().logout(true, null);
                Toast.makeText(getApplicationContext(), R.string.login_failure_failed, Toast.LENGTH_SHORT)
                        .show();
            }
        });
        return;
    }

    // ?nickname ios?nick
    boolean updatenick = EMChatManager.getInstance()
            .updateCurrentUserNick(SuperWeChatApplication.currentUserNick.trim());
    if (!updatenick) {
        Log.e("LoginActivity", "update current user nick fail");
    }
    if (!LoginActivity.this.isFinishing() && pd.isShowing()) {
        pd.dismiss();
    }
    // ?
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);

    finish();
}

From source file:cn.markmjw.platform.login.wechat.WechatLoginHandler.java

License:Apache License

/**
 * request token from wechat server.//w w w  .j av a  2  s.c o m
 *
 * @param code the auth code
 */
private void requestToken(String code) {
    Map<String, String> params = new HashMap<>();
    params.put("appid", PlatformConfig.getInstance().getWechatId());
    params.put("secret", PlatformConfig.getInstance().getWechatSecret());
    params.put("code", code);
    params.put("grant_type", "authorization_code");
    String url = HttpUtil.buildUrl(URL_TOKEN, params);
    final Request request = new Request.Builder().url(url).build();
    HttpUtil.enqueue(request, new Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            callBack(ILoginListener.CODE_AUTH_FAILED, e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                String data = response.body().string();
                WechatLoginResult result = GsonUtil.fromJson(data, WechatLoginResult.class);
                AuthResult auth = new AuthResult();
                auth.from = AuthResult.TYPE_WECHAT;
                auth.id = result.openid;
                auth.accessToken = result.accessToken;
                auth.expiresIn = result.expiresIn;
                auth.refreshToken = result.refreshToken;

                log("Wechat authorize success!" + "\nOpenId: " + auth.id + "\nAccess token: " + auth.accessToken
                        + "\nExpires in: " + formatDate(auth.expiresIn));

                callBack(ILoginListener.CODE_AUTH_SUCCESS, auth);
                if (mRequestInfoEnable) {
                    callBack(ILoginListener.CODE_LOGIN_ING, "");
                    // request user info
                    requestUserInfo(auth.id, auth.accessToken);
                }
            } else {
                callBack(ILoginListener.CODE_AUTH_FAILED, response.message());
            }
        }
    });
}

From source file:cn.markmjw.platform.login.wechat.WechatLoginHandler.java

License:Apache License

/**
 * request user information.//ww  w . j a v a 2s. co m
 *
 * @param openId      the app id
 * @param accessToken the access token
 */
private void requestUserInfo(String openId, String accessToken) {
    Map<String, String> params = new HashMap<>();
    params.put("access_token", accessToken);
    params.put("openid", openId);
    String url = HttpUtil.buildUrl(URL_WECHAT_USER, params);
    Request request = new Request.Builder().url(url).build();
    HttpUtil.enqueue(request, new Callback() {

        @Override
        public void onFailure(Request request, IOException e) {
            callBack(ILoginListener.CODE_FAILED, e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                String data = response.body().string();
                WechatUserInfo info = GsonUtil.fromJson(data, WechatUserInfo.class);
                callBack(ILoginListener.CODE_SUCCESS, info);
            } else {
                callBack(ILoginListener.CODE_FAILED, response.message());
            }
        }
    });
}

From source file:cn.markmjw.platform.login.weibo.WeiboLoginHandler.java

License:Apache License

/**
 * request user information./*w  w w .  j a va2s .c o  m*/
 *
 * @param result the login result.
 */
private void requestUserInfo(final WeiboLoginResult result) {
    Map<String, String> params = new HashMap<>();
    params.put("uid", result.uid);
    params.put("access_token", result.access_token);
    params.put("source", PlatformConfig.getInstance().getWeiboKey());
    String url = HttpUtil.buildUrl(URL_GET_USER_INFO, params);
    Request request = new Request.Builder().url(url).build();
    HttpUtil.enqueue(request, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            callBack(ILoginListener.CODE_FAILED, e.getMessage());
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                String data = response.body().string();
                WeiboUserInfo info = GsonUtil.fromJson(data, WeiboUserInfo.class);
                callBack(ILoginListener.CODE_SUCCESS, info);
            } else {
                callBack(ILoginListener.CODE_FAILED, response.message());
            }
        }
    });
}

From source file:cn.ucai.chatuidemo.activity.LoginActivity.java

License:Open Source License

private void downloadUserAvatar() {
    final OkHttpUtils2<Message> utils = new OkHttpUtils2<Message>();
    utils.url(UserUtils.getUserAvatarPath(currentUsername)).targetClass(Message.class)
            .doInBackground(new Callback() {
                @Override//from   w w  w  .  ja v  a 2s .  c  o m
                public void onFailure(Request request, IOException e) {
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    byte[] data = response.body().bytes();
                    final String avatarUrl = ((DemoHXSDKHelper) HXSDKHelper.getInstance())
                            .getUserProfileManager().uploadUserAvatar(data);
                    Log.e(TAG, "avatarUrl=" + avatarUrl);
                }
            }).execute(new OkHttpUtils2.OnCompleteListener<Message>() {
                @Override
                public void onSuccess(Message result) {
                    Log.e(TAG, "result=" + result);
                }

                @Override
                public void onError(String error) {
                    Log.e(TAG, "error=" + error);
                }
            });
}

From source file:cn.ucai.fulicenter.activity.LoginActivity.java

License:Open Source License

private void loginSuccess() {
    try {/*  w  w w  .j  a  v a2s  . c  om*/
        // ** ?logout???
        // ** manually load all local groups and
        EMGroupManager.getInstance().loadAllGroups();
        EMChatManager.getInstance().loadAllConversations();
        //?
        final OkHttpUtils<Message> utils = new OkHttpUtils<Message>();
        utils.url(BiyabiApplication.SERVER_ROOT)//??
                .addParam(I.KEY_REQUEST, I.REQUEST_DOWNLOAD_AVATAR)//?
                .addParam(I.AVATAR_TYPE, currentUsername)//?
                .doInBackground(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                        String avatarPath = I.AVATAR_TYPE_USER_PATH + I.BACKSLASH + currentUsername
                                + I.AVATAR_SUFFIX_JPG;
                        File file = OnSetAvatarListener.getAvatarFile(mContext, avatarPath);
                        FileOutputStream out = null;
                        out = new FileOutputStream(file);
                        utils.downloadFile(response, file, false);
                    }
                }).execute(null);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "start download contact,group,public group");
                //??
                new DownloadContactListTask(mContext, currentUsername).execute();
                //?
                new DownloadAllGroupTask(mContext, currentUsername).execute();
                //?
                new DownloadPublicGroupTask(mContext, currentUsername, I.PAGE_ID_DEFAULT, I.PAGE_SIZE_DEFAULT)
                        .execute();
            }
        });

        // ??
        initializeContacts();
    } catch (Exception e) {
        e.printStackTrace();
        // ?????
        runOnUiThread(new Runnable() {
            public void run() {
                pd.dismiss();
                DemoHXSDKHelper.getInstance().logout(true, null);
                Toast.makeText(getApplicationContext(), R.string.login_failure_failed, Toast.LENGTH_LONG)
                        .show();
            }
        });
        return;
    }
    // ?nickname ios?nick
    boolean updatenick = EMChatManager.getInstance()
            .updateCurrentUserNick(BiyabiApplication.currentUserNick.trim());
    if (!updatenick) {
        Log.e("LoginActivity", "update current user nick fail");
    }
    if (!LoginActivity.this.isFinishing() && pd.isShowing()) {
        pd.dismiss();
    }
    // ?
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);

    finish();
}

From source file:cn.ucai.git.activity.LoginActivity.java

License:Open Source License

private void save2SD() {
    final OkHttpUtils<Message> utils = new OkHttpUtils<Message>();
    utils.url(SuperWeChatApplication.ROOT_SERVER).addParam(I.KEY_REQUEST, I.REQUEST_DOWNLOAD_AVATAR)
            .addParam(I.AVATAR_TYPE, currentUsername).doInBackground(new Callback() {
                @Override/*from  w  ww . j  a v a2 s  .c o  m*/
                public void onFailure(Request request, IOException e) {

                }

                @Override
                public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                    String avatarPath = I.AVATAR_TYPE_USER_PATH + I.BACKSLASH + currentUsername
                            + I.AVATAR_SUFFIX_JPG;
                    File file = OnSetAvatarListener.getAvatarFile(LoginActivity.this, avatarPath);
                    FileOutputStream out = null;
                    out = new FileOutputStream(file);
                    utils.downloadFile(response, file, false);
                }
            }).execute(null);
}

From source file:cn.ucai.superkache.activity.LoginActivity.java

License:Open Source License

private void loginSuccess() {
    try {/*from w  w w .j  ava 2 s .co m*/
        // ** ?logout???
        // ** manually load all local groups and
        EMGroupManager.getInstance().loadAllGroups();
        EMChatManager.getInstance().loadAllConversations();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new DownloadContactListTask(mContext, currentUsername).execute();
                new DownloadAllGroupTask(mContext, currentUsername).execute();
                new DownloadPublicGroupTask(mContext, currentUsername, I.PAGE_ID_DEFAULT, I.PAGE_SIZE_DEFAULT)
                        .execute();
            }
        });

        // ??
        initializeContacts();
    } catch (Exception e) {
        e.printStackTrace();
        // ?????
        runOnUiThread(new Runnable() {
            public void run() {
                pd.dismiss();
                DemoHXSDKHelper.getInstance().logout(true, null);
                Toast.makeText(getApplicationContext(), R.string.login_failure_failed, Toast.LENGTH_LONG)
                        .show();
            }
        });
        return;
    }
    // ?nickname ios?nick
    boolean updatenick = EMChatManager.getInstance()
            .updateCurrentUserNick(SuperWeChatApplication.currentUserNick.trim());
    if (!updatenick) {
        Log.e("LoginActivity", "update current user nick fail");
    }
    if (!LoginActivity.this.isFinishing() && pd.isShowing()) {
        pd.dismiss();
    }
    // ?
    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(intent);
    //?SD?
    final OkHttpUtils<Message> utils = new OkHttpUtils<Message>();
    utils.url(SuperWeChatApplication.SERVER_ROOT).addParam(I.KEY_REQUEST, I.REQUEST_DOWNLOAD_AVATAR)
            .addParam(I.AVATAR_TYPE, currentUsername).doInBackground(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                    String avatarPath = I.AVATAR_TYPE_USER_PATH + I.BACKSLASH + currentUsername
                            + I.AVATAR_SUFFIX_JPG;
                    File file = OnSetAvatarListener.getAvatarFile(mContext, avatarPath);
                    FileOutputStream out = null;
                    out = new FileOutputStream(file);
                    utils.downloadFile(response, file, false);
                }
            }).execute(null);
    finish();
}

From source file:cn.ucai.superwechar.activity.LoginActivity.java

License:Open Source License

private void downloadAvatar() {
    //url= http://10.0.2.2:8080/SuperWeChatServer/Server?request=download_avatar&avatarType=
    final OkHttpUtils utils = new OkHttpUtils<>();
    utils.url(superwecharApplication.SERVER_ROOT).addParam(I.KEY_REQUEST, I.REQUEST_DOWNLOAD_AVATAR)
            .addParam(I.AVATAR_TYPE, currentUsername).doInBackground(new Callback() {
                @Override//  w w w.  ja v a 2  s .c  o  m
                public void onFailure(Request request, IOException e) {
                    Toast.makeText(mActivity, e.getMessage(), Toast.LENGTH_LONG).show();
                }

                @Override
                public void onResponse(com.squareup.okhttp.Response response) throws IOException {
                    String avatarPath = I.AVATAR_PATH + I.BACKSLASH + currentUsername + I.AVATAR_SUFFIX_JPG;
                    File file = OnSetAvatarListener.getAvatarFile(mActivity, avatarPath);
                    FileOutputStream fos = null;
                    fos = new FileOutputStream(file);
                    utils.downloadFile(response, file, false);
                }
            }).execute(null);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            new DownloadContactListTask(mContext, currentUsername).execute();
            new DownloadAllGroupTask(mContext, currentUsername).execute();
            new DownloadPublicGroupTask(mContext, currentUsername, I.PAGE_ID_DEFAULT, I.PAGE_SIZE_DEAULT)
                    .execute();
        }
    });
}