List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
From source file:pct.droid.base.updater.PopcornUpdater.java
License:Open Source License
private void downloadFile(final String location) { Request request = new Request.Builder().url(location).build(); mHttpClient.newCall(request).enqueue(new Callback() { @Override//w ww.j a v a 2 s.c o m public void onFailure(Request request, IOException e) { setChanged(); notifyObservers(STATUS_NO_UPDATE); } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { String fileName = location.substring(location.lastIndexOf('/') + 1); FileOutputStream fos = mContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE); fos.write(response.body().bytes()); fos.close(); PrefUtils.save(mContext, UPDATE_FILE, fileName); String update_file_path = mContext.getFilesDir().getAbsolutePath() + "/" + fileName; PrefUtils.getPrefs(mContext).edit().putString(SHA1_KEY, SHA1(update_file_path)) .putString(UPDATE_FILE, fileName).putLong(SHA1_TIME, System.currentTimeMillis()) .apply(); sendNotification(); setChanged(); notifyObservers(STATUS_HAVE_UPDATE); } else { setChanged(); notifyObservers(STATUS_NO_UPDATE); } } }); }
From source file:pct.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; }/* ww w. ja v a2 s. c o m*/ if (sCheckingRequest == null) { sCheckingRequest = new Request.Builder().head() .url("https://play.google.com/store/apps/details?id=ht.vpn.android").build(); PopcornApplication.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:studio.imedia.vehicleinspection.activity.LoginActivity.java
/** * //from w ww. j a va 2s .c o m * * @param url */ private void login(String url) { Log.d("reg", "login"); String phone = etPhoneNum.getText().toString(); String password = etPassword.getText().toString(); String urlGet = url + "?phone=" + phone + "&password=" + password; // ? final Request request = new Request.Builder().url(urlGet).build(); mClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { Log.d("reg", "onFailure"); e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { Log.d("reg", "not success"); throw new IOException("Unexpected code " + response); } String jsonStr = response.body().string(); Log.d("reg", "json " + jsonStr); getStatus(jsonStr); // ?json } }); }
From source file:studio.imedia.vehicleinspection.activity.PersonalInfoActivity.java
/** * ?/* w w w.j a v a 2s . c o m*/ * * @param urlSB * @param avatarPath */ private void uploadInfo(StringBuffer urlSB, String avatarPath) { Log.d("submit", "url " + urlSB.toString()); String url = urlSB.toString(); int id = (int) SPUtil.get(mContext, Constant.Key.USER_ID, Constant.Type.INTEGER); mUsername = tvUsername.getText().toString().trim(); String gender = tvGender.getText().toString(); if (gender.equals("")) mGender = MALE; else mGender = FEMALE; mSignature = etSignature.getText().toString().trim(); // ? FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder(); formEncodingBuilder.add("id", String.valueOf(id)).add("name", mUsername) .add("gender", String.valueOf(mGender)).add("signature", mSignature); if (isAvatarUpdate && avatarPath != null) formEncodingBuilder.add("avatar", avatarPath); RequestBody formBody = formEncodingBuilder.build(); final Request request = new Request.Builder().url(url).post(formBody).build(); mClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { Toast.makeText(mContext, "??", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String jsonStr = response.body().string(); Log.d("submit", "json " + jsonStr); try { int status = new JSONObject(jsonStr).getInt("status"); if (status == 0) mHandler.sendEmptyMessage(MSG_UPLOAD_INFO_SUCCESS); else mHandler.sendEmptyMessage(MSG_UPLOAD_INFO_FAIL); } catch (JSONException e) { e.printStackTrace(); } } }); }
From source file:studio.imedia.vehicleinspection.activity.PersonalInfoActivity.java
/** * ?/*from w w w . j av a 2s . c om*/ * * @param urlSB */ private void uploadAvatar(StringBuffer urlSB, File avatarFile) { String url = urlSB.toString(); String filename = avatarFile.getName(); File file = avatarFile; String avatarPath = null; // TODO // RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file); RequestBody fileBody = RequestBody.create(Constant.Media.TYPE_JPEG, file); RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM) .addPart(Headers.of("Content-Disposition", "form-data; name=\"uploadFile\""), fileBody).build(); Request request = new Request.Builder().url(url).post(requestBody).build(); mClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { mHandler.sendEmptyMessage(CONNECT_FAIL); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); } String jsonStr = response.body().string(); Log.d("files", jsonStr); try { int status = new JSONObject(jsonStr).getInt("status"); Log.d("files", "the status is " + status); } catch (JSONException e) { e.printStackTrace(); } } }); // TODO ?? // Message msg = new Message(); // msg.what = MSG_UPLOAD_AVATAR_SUCCESS; // msg.obj = avatarPath; // mHandler.sendMessage(msg); }
From source file:studio.imedia.vehicleinspection.fragments.CarInfoFragment.java
/** * ?/*ww w .j a v a2 s .c o m*/ */ private void uploadCarInfo(StringBuffer urlSB, String picPath) { String url = urlSB.toString(); // ?? mLicensePicPath = picPath; mDetailedAddress = tvCity.getText().toString().trim(); mEngineNum = etEngineNum.getText().toString().trim(); mRegisterTime = tvRegisterDate.getText().toString().trim(); // ?... FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder().add("id", String.valueOf(mId)) .add("carBrandId", String.valueOf(mCarBrandId)).add("carTypeId", String.valueOf(mCarTypeId)) .add("engineNum", mEngineNum).add("registerTime", mRegisterTime) .add("provinId", String.valueOf(mProvinceId)) // ?id .add("cityId", String.valueOf(mCityId)).add("countyId", String.valueOf(mCountyId)) .add("detailedAddress", mDetailedAddress); if (isLicenseUpdate && picPath != null) formEncodingBuilder.add("licensePic", mLicensePicPath); RequestBody formBody = formEncodingBuilder.build(); Request request = new Request.Builder().url(url).post(formBody).build(); mClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { mHandler.sendEmptyMessage(CONNECT_FAIL); } @Override public void onResponse(Response response) throws IOException { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String jsonStr = response.body().string(); try { int status = new JSONObject(jsonStr).getInt("status"); if (status == 0) { mHandler.sendEmptyMessage(MSG_UP_INFO_SUCCESS); } else { mHandler.sendEmptyMessage(MSG_UP_INFO_FAIL); } } catch (JSONException e) { e.printStackTrace(); } } }); }
From source file:themeable.images.AsyncFileDownloader.java
License:Apache License
private void startDownload(final String outputFileAbsolutePath, final Callback clientCallback, Request request) {/*from ww w . jav a 2s . c o m*/ client.newCall(request).enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException throwable) { Log.e(TAG, "Failed to download file from server", throwable); if (clientCallback != null) { clientCallback.onError(throwable, 0); } } @Override public void onResponse(Response response) { if (response.isSuccessful()) { try { File downloadedFile = new File(outputFileAbsolutePath); File dir = downloadedFile.getParentFile(); if (!dir.exists()) { dir.mkdirs(); } if (downloadedFile.exists()) { downloadedFile.delete(); } BufferedSink sink = Okio.buffer(Okio.sink(downloadedFile)); sink.writeAll(response.body().source()); sink.close(); Log.d(TAG, "Downloaded file to: " + downloadedFile.getAbsolutePath() + " " + downloadedFile.exists()); if (clientCallback != null) { clientCallback.onCompleted(downloadedFile); } } catch (IOException e) { if (clientCallback != null) { clientCallback.onError(e, response.code()); } } } else { if (clientCallback != null) { clientCallback.onError(null, response.code()); } } } }); }
From source file:uk.co.caprica.brue.okhttp.service.bridge.AbstractBridgeService.java
License:Open Source License
/** * Execute a request, synchronously./*www . j ava 2s .co m*/ * * @param request * @param responseType * @param <T> * @return */ private <T> T executeRequest(Request request, JavaType responseType) { try { Response response = httpClient.newCall(request).execute(); if (response.isSuccessful()) { if (responseType != null) { return response(response, responseType); } else { return null; } } else { throw new BridgeResponseException(response.code(), response.message()); } } catch (IOException e) { throw new BridgeIOException(e); } }
From source file:zblibrary.demo.manager.HttpRequest.java
License:Apache License
/** * @param client//www . j av a 2 s. co m * @param request * @return * @throws Exception */ private JSONObject getResponseObject(OkHttpClient client, Request request) throws Exception { if (client == null || request == null) { Log.e(TAG, "getResponseObject client == null || request == null >> return null;"); return null; } Response response = client.newCall(request).execute(); return response.isSuccessful() ? new JSONObject(response.body().string()) : null; }