Search in sources :

Example 81 with Callback

use of retrofit2.Callback in project Shuttle by timusus.

the class BiographyDialog method getBiographyDialog.

private static MaterialDialog getBiographyDialog(final Context context, @BioType int type, String artistName, String albumName) {
    @SuppressLint("InflateParams") View customView = LayoutInflater.from(context).inflate(R.layout.dialog_biography, null, false);
    final ProgressBar progressBar = customView.findViewById(R.id.progress);
    final TextView message = customView.findViewById(R.id.message);
    Callback<LastFmArtist> artistCallback = new Callback<LastFmArtist>() {

        @Override
        public void onResponse(Call<LastFmArtist> call, Response<LastFmArtist> response) {
            progressBar.setVisibility(View.GONE);
            if (response != null && response.isSuccessful()) {
                if (response.body() != null && response.body().artist != null && response.body().artist.bio != null) {
                    String summary = response.body().artist.bio.summary;
                    if (ShuttleUtils.hasNougat()) {
                        message.setText(Html.fromHtml(summary, Html.FROM_HTML_MODE_COMPACT));
                    } else {
                        message.setText(Html.fromHtml(summary));
                    }
                } else {
                    message.setText(R.string.no_artist_info);
                }
            }
        }

        @Override
        public void onFailure(Call<LastFmArtist> call, Throwable t) {
            progressBar.setVisibility(View.GONE);
            switch(type) {
                case BioType.ARTIST:
                    message.setText(R.string.no_artist_info);
                    break;
                case BioType.ALBUM:
                    message.setText(R.string.no_album_info);
                    break;
            }
        }
    };
    Callback<LastFmAlbum> albumCallback = new Callback<LastFmAlbum>() {

        @Override
        public void onResponse(Call<LastFmAlbum> call, Response<LastFmAlbum> response) {
            progressBar.setVisibility(View.GONE);
            if (response != null && response.isSuccessful()) {
                if (response.body() != null && response.body().album != null && response.body().album.wiki != null) {
                    String summary = response.body().album.wiki.summary;
                    if (ShuttleUtils.hasNougat()) {
                        message.setText(Html.fromHtml(summary, Html.FROM_HTML_MODE_COMPACT));
                    } else {
                        message.setText(Html.fromHtml(summary));
                    }
                } else {
                    message.setText(R.string.no_album_info);
                }
            }
        }

        @Override
        public void onFailure(Call<LastFmAlbum> call, Throwable t) {
            progressBar.setVisibility(View.GONE);
            switch(type) {
                case BioType.ARTIST:
                    message.setText(R.string.no_artist_info);
                    break;
                case BioType.ALBUM:
                    message.setText(R.string.no_album_info);
                    break;
            }
        }
    };
    switch(type) {
        case BioType.ARTIST:
            HttpClient.getInstance().lastFmService.getLastFmArtistResult(artistName).enqueue(artistCallback);
            break;
        case BioType.ALBUM:
            HttpClient.getInstance().lastFmService.getLastFmAlbumResult(artistName, albumName).enqueue(albumCallback);
            break;
    }
    MaterialDialog.Builder builder = DialogUtils.getBuilder(context).title(R.string.info).customView(customView, false).negativeText(R.string.close);
    return builder.build();
}
Also used : Call(retrofit2.Call) LastFmArtist(com.simplecity.amp_library.http.lastfm.LastFmArtist) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) LastFmAlbum(com.simplecity.amp_library.http.lastfm.LastFmAlbum) View(android.view.View) TextView(android.widget.TextView) Response(retrofit2.Response) Callback(retrofit2.Callback) SuppressLint(android.annotation.SuppressLint) TextView(android.widget.TextView) ProgressBar(android.widget.ProgressBar)

Example 82 with Callback

use of retrofit2.Callback in project baseio by generallycloud.

the class TestOkHttp method getAsynHttp.

static void getAsynHttp() {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    Request.Builder requestBuilder = new Request.Builder().url("https://www.aliyun.com/");
    // 可以省略,默认是GET请求
    requestBuilder.method("GET", null);
    Request request = requestBuilder.build();
    Call mcall = mOkHttpClient.newCall(request);
    mcall.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (null != response.cacheResponse()) {
                String str = response.cacheResponse().toString();
                logger.info("cache---{}" + str);
            } else {
                response.body().string();
                String str = response.networkResponse().toString();
                logger.info("network---{}" + str);
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)51 Response (okhttp3.Response)46 Call (okhttp3.Call)44 Callback (okhttp3.Callback)44 Request (okhttp3.Request)41 RequestBody (okhttp3.RequestBody)25 Call (retrofit2.Call)17 Callback (retrofit2.Callback)15 Test (org.junit.Test)14 Response (retrofit2.Response)14 CountDownLatch (java.util.concurrent.CountDownLatch)13 FormBody (okhttp3.FormBody)12 ToStringConverterFactory (retrofit2.helpers.ToStringConverterFactory)12 OkHttpClient (okhttp3.OkHttpClient)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 ResponseBody (okhttp3.ResponseBody)10 MockResponse (okhttp3.mockwebserver.MockResponse)10 Retrofit (retrofit2.Retrofit)9 TextView (android.widget.TextView)6 HttpUrl (okhttp3.HttpUrl)6