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:abtlibrary.utils.as24ApiClient.ApiClient.java

License:Apache License

/**
 * Execute HTTP call asynchronously./*from w  ww . ja  va  2  s .  c  o m*/
 *
 * @see #execute(Call, Type)
 * @param <T> Type
 * @param call The callback to be executed when the API call finishes
 * @param returnType Return type
 * @param callback ApiCallback
 */
@SuppressWarnings("unchecked")
public <T> void executeAsync(Call call, final Type returnType, final ApiCallback<T> callback) {
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            callback.onFailure(new ApiException(e), 0, null);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            T result;
            try {
                result = (T) handleResponse(response, returnType);
            } catch (ApiException e) {
                callback.onFailure(e, response.code(), response.headers().toMultimap());
                return;
            }
            callback.onSuccess(result, response.code(), response.headers().toMultimap());
        }
    });
}

From source file:appewtc.masterung.testdrivinglicense.ConfirmScoreActivity.java

public void clickOKConfirm(View view) {

    String urlPHP = "http://swiftcodingthai.com/toey/add_score.php";

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0])
            .add("Date", dateString).add("Score", scoreString).build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override//from   ww w  .ja va2s  . c o  m
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

            Intent intent = new Intent(ConfirmScoreActivity.this, ScoreListView.class);
            intent.putExtra("login", loginStrings);
            startActivity(intent);
            finish();

        }
    });

}

From source file:appewtc.masterung.testdrivinglicense.ScoreListView.java

private void createListView() {

    String urlPHP = "http://swiftcodingthai.com/toey/get_score_where.php";

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0])
            .build();/* www . j  av  a  2s .c  o m*/
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

            String strResponse = response.body().string();
            Log.d("10AugV2", "strResponse ==> " + strResponse);

            try {

                JSONArray jsonArray = new JSONArray(strResponse);

                String[] dateStrings = new String[jsonArray.length()];
                String[] scoreStrings = new String[jsonArray.length()];

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    dateStrings[i] = jsonObject.getString("Date");
                    scoreStrings[i] = "? = " + jsonObject.getString("Score")
                            + " ?";

                } // for

                CoaurseAdapter coaurseAdapter = new CoaurseAdapter(ScoreListView.this, 1, dateStrings,
                        scoreStrings);
                listView.setAdapter(coaurseAdapter);

            } catch (Exception e) {
                e.printStackTrace();
            }

        } // onResponse
    });

}

From source file:appewtc.masterung.testdrivinglicense.SignUpActivity.java

private void upLoadNewUser() {

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("Name", nameString)
            .add("Surname", surnameString).add("Age", ageString).add("User", userString)
            .add("Password", passwordString).build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override/*from  w w w .jav a 2  s  .c  o m*/
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

            finish();

        }
    });

}

From source file:butter.droid.activities.MainActivity.java

License:Open Source License

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

    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   ww  w .j a v a 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(MainActivity.this);
                dialogInput.setText(
                        "http://download.wavetlan.com/SVV/Media/HTTP/MP4/ConvertedFiles/QuickTime/QuickTime_test13_5m19s_AVC_VBR_324kbps_640x480_25fps_AAC-LCv4_CBR_93.4kbps_Stereo_44100Hz.mp4");
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setView(dialogInput)
                        .setPositiveButton("Start", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Movie media = new Movie(new MoviesProvider(), new YSubsProvider());

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

                                String location = dialogInput.getText().toString();

                                BeamManager bm = BeamManager.getInstance(MainActivity.this);
                                if (bm.isConnected()) {
                                    BeamPlayerActivity.startActivity(MainActivity.this,
                                            new StreamInfo(media, null, null, null, null, location), 0);
                                } else {
                                    VideoPlayerActivity.startActivity(MainActivity.this,
                                            new StreamInfo(media, null, null, null, null, location), 0);
                                }
                            }
                        });
                builder.show();
            } else if (YouTubeData.isYouTubeUrl(location)) {
                Intent i = new Intent(MainActivity.this, TrailerPlayerActivity.class);
                Movie media = new Movie(new MoviesProvider(), new YSubsProvider());
                media.title = file_types[index];
                i.putExtra(TrailerPlayerActivity.DATA, media);
                i.putExtra(TrailerPlayerActivity.LOCATION, location);
                startActivity(i);
            } else {
                final Movie media = new Movie(new MoviesProvider(), 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(MainActivity.this, media, "en", new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        BeamManager bm = BeamManager.getInstance(MainActivity.this);

                        if (bm.isConnected()) {
                            BeamPlayerActivity.startActivity(MainActivity.this,
                                    new StreamInfo(media, null, null, null, null, location), 0);
                        } else {
                            VideoPlayerActivity.startActivity(MainActivity.this,
                                    new StreamInfo(media, null, null, null, null, location), 0);
                        }
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        BeamManager bm = BeamManager.getInstance(MainActivity.this);
                        if (bm.isConnected()) {
                            BeamPlayerActivity.startActivity(MainActivity.this,
                                    new StreamInfo(media, null, null, "en", null, location), 0);
                        } else {
                            VideoPlayerActivity.startActivity(MainActivity.this,
                                    new StreamInfo(media, null, null, "en", null, location), 0);
                        }
                    }
                });
            }
        }
    });

    builder.show();
}

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 ww .j a  v  a2  s .  c  om

    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:butter.droid.tv.fragments.TVOverviewFragment.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//  w w  w . j  a v  a  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 MoviesProvider(), new YSubsProvider());

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

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

            final Movie media = new Movie(new MoviesProvider(), 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) {
                    TVVideoPlayerActivity.startActivity(getActivity(),
                            new StreamInfo(media, null, null, null, null, location));
                }

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

    builder.show();
}

From source file:cc.arduino.mvd.services.HttpService.java

License:Apache License

/**
 * Perform a POST request to the service
 *
 * @param url/*from   w w w.j  a v  a 2s .com*/
 * @param json
 * @return
 * @throws IOException
 */
private void post(String url, String json) {
    MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    RequestBody body = RequestBody.create(JSON, json);

    Log.d(TAG, "POST: " + url);

    final Request request = new Request.Builder().url(url).post(body).build();

    Call call = client.newCall(request);

    call.enqueue(new Callback() {
        @Override
        public void onFailure(final Request request, final IOException exception) {
            // Meh, don't do anything...
            exception.printStackTrace();
        }

        @Override
        public void onResponse(final Response response) throws IOException {
            // Meh, don't do anything...
            Log.d(TAG, request.toString());
        }
    });
}

From source file:cc.arduino.mvd.services.HttpService.java

License:Apache License

/**
 * Get something from the service/*  w ww.j  av  a2s  .co m*/
 *
 * @param url
 * @return
 * @throws IOException
 */
private void get(String url) throws IOException {
    MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    final Request request = new Request.Builder().url(url).get().build();

    Call call = client.newCall(request);

    call.enqueue(new Callback() {
        @Override
        public void onFailure(final Request request, final IOException exception) {
            // Meh, don't do anything...
            exception.printStackTrace();
        }

        @Override
        public void onResponse(final Response response) throws IOException {
            try {
                String body = new String(response.body().bytes());
                JSONObject json = new JSONObject(body);
                handleKeyValFromHttp(json.getString("code"), json.getString("pin"), json.getString("value"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
}

From source file:cn.edu.zafu.news.ui.main.NewsFragment.java

License:Apache License

private void refreshData() {
    isRefreshing = true;/*from   w  w w . j av  a2  s.c  om*/
    page = 1;
    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(REFRESH_ERROR);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            NewsParser newsParser = new NewsParser();
            String str = response.body().string();
            if (max == 0) {
                getMaxPage(str);
            }
            list.clear();
            list.addAll(newsParser.convert(str));
            handler.sendEmptyMessage(REFRESH);
        }
    });

}