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: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//from   ww w .ja 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;
    }/* w w w  .  j  a v  a 2 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:pct.droid.tv.fragments.PTVOverviewFragment.java

License:Open Source License

private void openPlayerTestDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final String[] file_types = getResources().getStringArray(R.array.file_types);
    final String[] files = getResources().getStringArray(R.array.files);

    builder.setTitle("Player Tests").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override//from  w  w  w.  j  a va 2  s.co m
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    }).setSingleChoiceItems(file_types, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int index) {
            dialogInterface.dismiss();
            final String location = files[index];
            if (location.equals("dialog")) {
                final EditText dialogInput = new EditText(getActivity());
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setView(dialogInput)
                        .setPositiveButton("Start", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Movie media = new Movie(new YTSProvider(), new YSubsProvider());

                                media.videoId = "dialogtestvideo";
                                media.title = "User input test video";

                                PTVVideoPlayerActivity.startActivity(getActivity(),
                                        new StreamInfo(media, null, null, null, null, location), 0);
                            }
                        });
                builder.show();
            }

            final Movie media = new Movie(new YTSProvider(), new YSubsProvider());
            media.videoId = "bigbucksbunny";
            media.title = file_types[index];
            media.subtitles = new HashMap<>();
            media.subtitles.put("en", "http://sv244.cf/bbb-subs.srt");

            SubsProvider.download(getActivity(), media, "en", new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    PTVVideoPlayerActivity.startActivity(getActivity(),
                            new StreamInfo(media, null, null, null, null, location));
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    PTVVideoPlayerActivity.startActivity(getActivity(),
                            new StreamInfo(media, null, null, null, null, location));
                }
            });
        }
    });

    builder.show();
}

From source file:studio.imedia.vehicleinspection.activity.LoginActivity.java

/**
 * //from  ww w.  j  a v a  2s . co  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

/**
 * ?//from   w  w  w .j a  va 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

/**
 * ?// w ww  .j a va  2 s  . c  o m
 *
 * @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

/**
 * ?//from  www . j a  v  a2  s.  co 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();
            }
        }
    });
}