List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
From source file:butter.droid.base.providers.subs.YSubsProvider.java
License:Open Source License
private void fetch(Request.Builder requestBuilder, final Movie media, final Callback callback) { enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() { @Override/*ww w .ja v a 2s . co m*/ public void onFailure(Request request, IOException e) { callback.onFailure(e); } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { String responseStr = response.body().string(); YSubsResponse result = mGson.fromJson(responseStr, YSubsResponse.class); callback.onSuccess(result.formatForPopcorn(PREFIX, LANGUAGE_MAPPING).get(media.imdbId)); } } }); }
From source file:butter.droid.base.vpn.VPNHTChecker.java
License:Open Source License
public static boolean isDownloadAvailable(final Context context) { if (PackageUtils.isInstalled(context, VPNManager.PACKAGE_VPNHT) || PrefUtils.get(context, VPN_AVAILABLE, false)) { return true; }/*from w w w . ja v a 2 s.com*/ if (sCheckingRequest == null) { sCheckingRequest = new Request.Builder().head() .url("https://play.google.com/store/apps/details?id=ht.vpn.android").build(); ButterApplication.getHttpClient().newCall(sCheckingRequest).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { sCheckingRequest = null; PrefUtils.save(context, VPN_AVAILABLE, false); } @Override public void onResponse(Response response) throws IOException { sCheckingRequest = null; if (response.isSuccessful()) { PrefUtils.save(context, VPN_AVAILABLE, true); } else { PrefUtils.save(context, VPN_AVAILABLE, false); } } }); } return false; }
From source file:cn.finalteam.okhttpfinal.dm.DownloadHttpTask.java
License:Apache License
private int request() { String url = mDownloadInfo.getUrl(); int resultCode = RESULT_SUCCESS; long startPos = 0; String fileName = FileUtils.getUrlFileName(url); File file = new File(mDownloadInfo.getTargetFolder(), fileName); if (StringUtils.isEmpty(mDownloadInfo.getTargetPath())) { mDownloadInfo.setTargetPath(file.getAbsolutePath()); }// w w w . java2s .c o m if (file.exists()) { startPos = file.length(); } else { try { boolean b = file.createNewFile(); if (!b) { resultCode = RESULT_OTHER_ERROR; Logger.e("create new File failure file=" + file.getAbsolutePath()); return resultCode; } } catch (IOException e) { Logger.e(e + " file=" + file.getAbsolutePath()); resultCode = RESULT_OTHER_ERROR; return resultCode; } } // try { mProgressReportingRandomAccessFile = new ProgressReportingRandomAccessFile(file, "rw", startPos); } catch (FileNotFoundException e) { Logger.e(e); resultCode = RESULT_OTHER_ERROR; return resultCode; } Request request = new Request.Builder().url(url).header("RANGE", "bytes=" + startPos + "-")//httpRANGE .build(); // Response response = null; try { response = mOkHttpClient.newCall(request).execute(); } catch (IOException e) { Logger.e(e); resultCode = RESULT_OTHER_ERROR; return resultCode; } if (response == null || !response.isSuccessful()) {//?? if (response != null) { Logger.e("~ code=" + response.code() + "url=" + url); } resultCode = RESULT_NET_ERROR; } else { try { InputStream inputStream = response.body().byteStream(); try { long totalLength = response.body().contentLength(); if (mDownloadInfo.getTotalLength() == 0l) { mDownloadInfo.setTotalLength(totalLength); } //?? if (startPos > mDownloadInfo.getTotalLength()) { FileUtils.deleteFile(mDownloadInfo.getTargetPath()); mDownloadInfo.setProgress(0); mDownloadInfo.setDownloadLength(0); mDownloadInfo.setTotalLength(0); return resultCode; } if (startPos == mDownloadInfo.getTotalLength() && startPos > 0) { publishProgress(100); return resultCode; } //? int bytesCopied = download(inputStream, mProgressReportingRandomAccessFile); if (((startPos + bytesCopied) != mDownloadInfo.getTotalLength()) || mInterrupt) { // resultCode = RESULT_OTHER_ERROR; return resultCode; } } catch (Exception e) { e.printStackTrace(); resultCode = RESULT_OTHER_ERROR; return resultCode; } } catch (IOException e) { e.printStackTrace(); resultCode = RESULT_OTHER_ERROR; return resultCode; } } return resultCode; }
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 ww w. j a va 2s .c o m*/ //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./*from w w w . j av a2 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./*from ww w. j a v a 2 s . c om*/ * * @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.//ww w. ja v a 2 s .c om * * @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:com.android.cloudfiles.cloudfilesforandroid.CloudFiles.java
License:Apache License
public Response download(String region, String bucket, String key, String target) throws IOException { String string = getObjectCdnUrl(region, bucket, key); Request request = new Request.Builder().url(string) .get().build();// w w w.j a v a 2 s. c o m Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); FileOutputStream f = new FileOutputStream(target); BufferedSink sink = Okio.buffer(Okio.sink(f)); sink.write(response.body().bytes()); sink.close(); return response; }
From source file:com.android.cloudfiles.cloudfilesforandroid.CloudFiles.java
License:Apache License
private Response createBucket(String region, String bucket) throws IOException { RequestBody body = RequestBody.create(IMAGE, ""); Request request = new Request.Builder().url(urls.get(region) + "/" + bucket) .header("X-Auth-Token", getToken()).put(body).build(); client.newCall(request).execute();/* ww w . j ava2 s.com*/ body = RequestBody.create(IMAGE, ""); request = new Request.Builder().url(cdnUrls.get(region) + "/" + bucket).header("X-Auth-Token", getToken()) .addHeader("X-Cdn-Enabled", "True").put(body).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); return response; }
From source file:com.android.cloudfiles.cloudfilesforandroid.CloudFiles.java
License:Apache License
private String generateToken(String userName, String apiKey) throws IOException { JsonObject cred = new JsonObject(); cred.addProperty("username", userName); cred.addProperty("apiKey", apiKey); JsonObject rax = new JsonObject(); rax.add("RAX-KSKEY:apiKeyCredentials", cred); JsonObject obj = new JsonObject(); obj.add("auth", rax); String json = obj.toString(); RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder().url(tokenURL).post(body).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); return response.body().string(); }