List of usage examples for com.squareup.okhttp Response body
ResponseBody body
To view the source code for com.squareup.okhttp Response body.
Click Source Link
From source file:cn.finalteam.okhttpfinal.FileDownloadTask.java
License:Apache License
@Override protected Boolean doInBackground(Void... params) { ////from w ww. ja v a 2s .c o m final Request request = new Request.Builder().url(url).build(); boolean suc = false; try { Response response = okHttpClient.newCall(request).execute(); long total = response.body().contentLength(); saveFile(response); if (total == target.length()) { suc = true; } } catch (Exception e) { e.printStackTrace(); suc = false; } return suc; }
From source file:cn.finalteam.okhttpfinal.FileDownloadTask.java
License:Apache License
public String saveFile(Response response) throws IOException { InputStream is = null;/* w ww. java 2s . c o m*/ byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; try { is = response.body().byteStream(); final long total = response.body().contentLength(); long sum = 0; FileUtils.makeFolders(target); fos = new FileOutputStream(target); while ((len = is.read(buf)) != -1) { sum += len; fos.write(buf, 0, len); if (callback != null) { publishProgress(sum, total); } } fos.flush(); return target.getAbsolutePath(); } finally { try { if (is != null) { is.close(); } } catch (IOException e) { } try { if (fos != null) { fos.close(); } } catch (IOException e) { } } }
From source file:cn.finalteam.okhttpfinal.HttpTask.java
License:Apache License
@Override protected ResponseData doInBackground(Void... voids) { Response response = null; ResponseData responseData = new ResponseData(); try {//from w w w.j a va 2 s . com //OkHttpClient client = OkHttpFactory.getOkHttpClientFactory(timeout); String srcUrl = url; //Request Request.Builder builder = new Request.Builder(); switch (method) { case GET: url = Utils.getFullUrl(url, params.getUrlParams()); builder.get(); break; case DELETE: url = Utils.getFullUrl(url, params.getUrlParams()); builder.delete(); break; case HEAD: url = Utils.getFullUrl(url, params.getUrlParams()); builder.head(); break; case POST: RequestBody body = params.getRequestBody(); if (body != null) { builder.post(new ProgressRequestBody(body, callback)); } break; case PUT: RequestBody bodyPut = params.getRequestBody(); if (bodyPut != null) { builder.put(new ProgressRequestBody(bodyPut, callback)); } break; case PATCH: RequestBody bodyPatch = params.getRequestBody(); if (bodyPatch != null) { builder.put(new ProgressRequestBody(bodyPatch, callback)); } break; } builder.url(url).tag(srcUrl).headers(headers); Request request = builder.build(); if (Constants.DEBUG) { Logger.d("url=" + url + "?" + params.toString()); } // response = okHttpClient.newCall(request).execute(); } catch (Exception e) { if (Constants.DEBUG) { Logger.e("Exception=", e); } if (e instanceof SocketTimeoutException) { responseData.setTimeout(true); } else if (e instanceof InterruptedIOException && TextUtils.equals(e.getMessage(), "timeout")) { responseData.setTimeout(true); } } //? if (response != null) { responseData.setResponseNull(false); responseData.setCode(response.code()); responseData.setMessage(response.message()); responseData.setSuccess(response.isSuccessful()); String respBody = ""; try { respBody = response.body().string(); } catch (IOException e) { e.printStackTrace(); } responseData.setResponse(respBody); responseData.setHeaders(response.headers()); } else { responseData.setResponseNull(true); } return responseData; }
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 om*/ * * @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./* www . java 2 s . c o 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 av a2 s . 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. j a va2 s . c om 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.superwechart.activity.LoginActivity.java
License:Open Source License
private void downloadUserAvatar() { OkHttpUtils2<Message> utils = new OkHttpUtils2(); utils.url(UserUtils.getContactAvatarPath(currentUsername)).targetClass(Message.class) .doInBackground(new Callback() { @Override//from w w w . j a v a 2s . com public void onFailure(Request request, IOException e) { Log.e(TAG, "IOException=" + e); } @Override public void onResponse(Response response) throws IOException { byte[] data = response.body().bytes(); 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 result) { Log.e(TAG, "result=" + result); } }); }
From source file:cn.ucai.SuperWechat.activity.LoginActivity.java
License:Open Source License
private void UpdateUserAvatar() { final OkHttpUtils2<Message> utils = new OkHttpUtils2<Message>(); utils.setRequestUrl(I.REQUEST_DOWNLOAD_AVATAR).addParam(I.NAME_OR_HXID, currentUsername) .addParam(I.AVATAR_TYPE, I.AVATAR_TYPE_USER_PATH).targetClass(Message.class) .doInBackground(new Callback() { @Override//from www . j a v a 2s .c o m public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { byte[] bytes = response.body().bytes(); final String avatarUrl = ((DemoHXSDKHelper) HXSDKHelper.getInstance()) .getUserProfileManager().uploadUserAvatar(bytes); } }); }
From source file:cn.wochu.wh.net.OkHttpClientManager.java
License:Apache License
/** * ?Get//from w w w.j ava 2s. c o m * * @param url * @return */ private String _getAsString(String url) throws IOException { Response execute = _getAsyn(url); return execute.body().string(); }