Example usage for com.squareup.okhttp Request.Builder url

List of usage examples for com.squareup.okhttp Request.Builder url

Introduction

In this page you can find the example usage for com.squareup.okhttp Request.Builder url.

Prototype

HttpUrl url

To view the source code for com.squareup.okhttp Request.Builder url.

Click Source Link

Usage

From source file:butter.droid.base.providers.media.AnimeProvider.java

License:Open Source License

@Override
public Call getDetail(ArrayList<Media> currentList, Integer index, final Callback callback) {
    Request.Builder requestBuilder = new Request.Builder();
    String url = API_URLS[CURRENT_API] + "anime/" + currentList.get(index).videoId;
    requestBuilder.url(url);
    requestBuilder.tag(MEDIA_CALL);/*from  w ww .j  a  va 2s  . co m*/

    Timber.d("AnimeProvider", "Making request to: " + url);

    return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() {
        @Override
        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();
                LinkedTreeMap<String, Object> map = mGson.fromJson(responseStr, LinkedTreeMap.class);
                AnimeResponse result = new AnimeResponse(map);
                if (map == null) {
                    callback.onFailure(new NetworkErrorException("Empty response"));
                } else {
                    ArrayList<Media> formattedData = result.formatDetailForPopcorn();

                    if (formattedData.size() > 0) {
                        callback.onSuccess(null, formattedData, true);
                        return;
                    }
                    callback.onFailure(new IllegalStateException("Empty list"));
                    return;
                }
            }
            callback.onFailure(new NetworkErrorException("Couldn't connect to AnimeAPI"));
        }
    });
}

From source file:butter.droid.base.providers.media.MoviesProvider.java

License:Open Source License

@Override
public Call getList(final ArrayList<Media> existingList, Filters filters, final Callback callback) {
    final ArrayList<Media> currentList;
    if (existingList == null) {
        currentList = new ArrayList<>();
    } else {/*from  w  w  w .j ava 2 s . c o  m*/
        currentList = (ArrayList<Media>) existingList.clone();
    }

    ArrayList<NameValuePair> params = new ArrayList<>();
    params.add(new NameValuePair("limit", "30"));

    if (filters == null) {
        filters = new Filters();
    }

    if (filters.keywords != null) {
        params.add(new NameValuePair("keywords", filters.keywords));
    }

    if (filters.genre != null) {
        params.add(new NameValuePair("genre", filters.genre));
    }

    if (filters.order == Filters.Order.ASC) {
        params.add(new NameValuePair("order", "1"));
    } else {
        params.add(new NameValuePair("order", "-1"));
    }

    if (filters.langCode != null) {
        params.add(new NameValuePair("lang", filters.langCode));
    }

    String sort;
    switch (filters.sort) {
    default:
    case POPULARITY:
        sort = "popularity";
        break;
    case YEAR:
        sort = "year";
        break;
    case DATE:
        sort = "last added";
        break;
    case RATING:
        sort = "rating";
        break;
    case ALPHABET:
        sort = "name";
        break;
    case TRENDING:
        sort = "trending";
        break;
    }

    params.add(new NameValuePair("sort", sort));

    String url = API_URLS[CURRENT_API] + "movies/";
    if (filters.page != null) {
        url += filters.page;
    } else {
        url += "1";
    }

    Request.Builder requestBuilder = new Request.Builder();
    String query = buildQuery(params);
    url = url + "?" + query;
    requestBuilder.url(url);
    requestBuilder.tag(MEDIA_CALL);

    Timber.d("MoviesProvider", "Making request to: " + url);

    return fetchList(currentList, requestBuilder, filters, callback);
}

From source file:butter.droid.base.providers.media.MoviesProvider.java

License:Open Source License

/**
 * Fetch the list of movies from API/*w  w w .jav  a2  s. c  o  m*/
 *
 * @param currentList    Current shown list to be extended
 * @param requestBuilder Request to be executed
 * @param callback       Network callback
 * @return Call
 */
private Call fetchList(final ArrayList<Media> currentList, final Request.Builder requestBuilder,
        final Filters filters, final Callback callback) {
    return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            String url = requestBuilder.build().urlString();
            if (CURRENT_API >= API_URLS.length - 1) {
                callback.onFailure(e);
            } else {
                if (url.contains(API_URLS[CURRENT_API])) {
                    url = url.replace(API_URLS[CURRENT_API], API_URLS[CURRENT_API + 1]);
                    CURRENT_API++;
                } else {
                    url = url.replace(API_URLS[CURRENT_API - 1], API_URLS[CURRENT_API]);
                }
                requestBuilder.url(url);
                fetchList(currentList, requestBuilder, filters, callback);
            }
        }

        @Override
        public void onResponse(Response response) throws IOException {
            try {
                if (response.isSuccessful()) {
                    String responseStr = response.body().string();

                    ArrayList<LinkedTreeMap<String, Object>> list = null;
                    if (responseStr.isEmpty()) {
                        list = new ArrayList<>();
                    } else {
                        list = (ArrayList<LinkedTreeMap<String, Object>>) mGson.fromJson(responseStr,
                                ArrayList.class);
                    }

                    MovieResponse result = new MovieResponse(list);
                    if (list == null) {
                        callback.onFailure(new NetworkErrorException("Empty response"));
                    } else {
                        ArrayList<Media> formattedData = result.formatListForPopcorn(currentList);
                        callback.onSuccess(filters, formattedData, list.size() > 0);
                        return;
                    }
                }
            } catch (Exception e) {
                callback.onFailure(e);
            }
            callback.onFailure(new NetworkErrorException("Couldn't connect to MovieAPI"));
        }
    });
}

From source file:butter.droid.base.providers.media.TVProvider.java

License:Open Source License

@Override
public Call getList(final ArrayList<Media> existingList, Filters filters, final Callback callback) {
    final ArrayList<Media> currentList;
    if (existingList == null) {
        currentList = new ArrayList<>();
    } else {//from  ww  w. j  av  a2 s.co  m
        currentList = (ArrayList<Media>) existingList.clone();
    }

    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("limit", "30"));

    if (filters == null) {
        filters = new Filters();
    }

    if (filters.keywords != null) {
        params.add(new NameValuePair("keywords", filters.keywords));
    }

    if (filters.genre != null) {
        params.add(new NameValuePair("genre", filters.genre));
    }

    if (filters.order == Filters.Order.ASC) {
        params.add(new NameValuePair("order", "1"));
    } else {
        params.add(new NameValuePair("order", "-1"));
    }

    String sort = "";
    switch (filters.sort) {
    default:
    case POPULARITY:
        sort = "popularity";
        break;
    case TRENDING:
        sort = "trending";
        break;
    case YEAR:
        sort = "year";
        break;
    case DATE:
        sort = "updated";
        break;
    case RATING:
        sort = "rating";
        break;
    case ALPHABET:
        sort = "name";
        break;
    }

    params.add(new NameValuePair("sort", sort));

    String url = API_URLS[CURRENT_API] + "shows/";
    if (filters.page != null) {
        url += filters.page;
    } else {
        url += "1";
    }

    Request.Builder requestBuilder = new Request.Builder();
    String query = buildQuery(params);
    url = url + "?" + query;
    requestBuilder.url(url);
    requestBuilder.tag(MEDIA_CALL);

    Timber.d("TVProvider", "Making request to: " + url);

    return fetchList(currentList, requestBuilder, filters, callback);
}

From source file:butter.droid.base.providers.media.TVProvider.java

License:Open Source License

/**
 * Fetch the list of movies from EZTV/*  w  w  w  . j  a  v  a2  s  .com*/
 *
 * @param currentList    Current shown list to be extended
 * @param requestBuilder Request to be executed
 * @param callback       Network callback
 * @return Call
 */
private Call fetchList(final ArrayList<Media> currentList, final Request.Builder requestBuilder,
        final Filters filters, final Callback callback) {
    return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            String url = requestBuilder.build().urlString();
            if (CURRENT_API >= API_URLS.length - 1) {
                callback.onFailure(e);
            } else {
                if (url.contains(API_URLS[CURRENT_API])) {
                    url = url.replace(API_URLS[CURRENT_API], API_URLS[CURRENT_API + 1]);
                    CURRENT_API++;
                } else {
                    url = url.replace(API_URLS[CURRENT_API - 1], API_URLS[CURRENT_API]);
                }
                requestBuilder.url(url);
                fetchList(currentList, requestBuilder, filters, callback);
            }
        }

        @Override
        public void onResponse(Response response) throws IOException {
            try {
                if (response.isSuccessful()) {
                    String responseStr = response.body().string();

                    ArrayList<LinkedTreeMap<String, Object>> list = null;
                    if (responseStr.isEmpty()) {
                        list = new ArrayList<>();
                    } else {
                        list = (ArrayList<LinkedTreeMap<String, Object>>) mGson.fromJson(responseStr,
                                ArrayList.class);
                    }

                    TVReponse result = new TVReponse(list);
                    if (list == null) {
                        callback.onFailure(new NetworkErrorException("Empty response"));
                    } else {
                        ArrayList<Media> formattedData = result.formatListForPopcorn(currentList);
                        callback.onSuccess(filters, formattedData, list.size() > 0);
                        return;
                    }
                }
            } catch (Exception e) {
                callback.onFailure(e);
            }
            callback.onFailure(new NetworkErrorException("Couldn't connect to TVAPI"));
        }
    });
}

From source file:butter.droid.base.providers.media.TVProvider.java

License:Open Source License

@Override
public Call getDetail(ArrayList<Media> currentList, Integer index, final Callback callback) {
    Request.Builder requestBuilder = new Request.Builder();
    String url = API_URLS[CURRENT_API] + "show/" + currentList.get(index).videoId;
    requestBuilder.url(url);
    requestBuilder.tag(MEDIA_CALL);/*  w  w  w  . j a  v a 2  s  .  c om*/

    Timber.d("TVProvider", "Making request to: " + url);

    return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() {
        @Override
        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();
                LinkedTreeMap<String, Object> map = mGson.fromJson(responseStr, LinkedTreeMap.class);
                TVReponse result = new TVReponse(map);
                if (map == null) {
                    callback.onFailure(new NetworkErrorException("Empty response"));
                } else {
                    ArrayList<Media> formattedData = result.formatDetailForPopcorn();

                    if (formattedData.size() > 0) {
                        callback.onSuccess(null, formattedData, true);
                        return;
                    }
                    callback.onFailure(new IllegalStateException("Empty list"));
                    return;
                }
            }
            callback.onFailure(new NetworkErrorException("Couldn't connect to TVAPI"));
        }
    });
}

From source file:butter.droid.base.providers.media.VodoProvider.java

License:Open Source License

@Override
public Call getList(final ArrayList<Media> existingList, Filters filters, final Callback callback) {
    sFilters = filters;/* w ww .jav a2s .c om*/

    final ArrayList<Media> currentList;
    if (existingList == null) {
        currentList = new ArrayList<>();
    } else {
        currentList = (ArrayList<Media>) existingList.clone();
    }

    ArrayList<NameValuePair> params = new ArrayList<>();
    params.add(new NameValuePair("limit", "30"));

    if (filters == null) {
        filters = new Filters();
    }

    if (filters.keywords != null) {
        params.add(new NameValuePair("query_term", filters.keywords));
    }

    if (filters.genre != null) {
        params.add(new NameValuePair("genre", filters.genre));
    }

    if (filters.order == Filters.Order.ASC) {
        params.add(new NameValuePair("order_by", "asc"));
    } else {
        params.add(new NameValuePair("order_by", "desc"));
    }

    if (filters.langCode != null) {
        params.add(new NameValuePair("lang", filters.langCode));
    }

    String sort;
    switch (filters.sort) {
    default:
    case POPULARITY:
        sort = "seeds";
        break;
    case YEAR:
        sort = "year";
        break;
    case DATE:
        sort = "date_added";
        break;
    case RATING:
        sort = "rating";
        break;
    case ALPHABET:
        sort = "title";
        break;
    case TRENDING:
        sort = "trending_score";
        break;
    }

    params.add(new NameValuePair("sort_by", sort));

    if (filters.page != null) {
        params.add(new NameValuePair("page", Integer.toString(filters.page)));
    }

    Request.Builder requestBuilder = new Request.Builder();
    String query = "?" + buildQuery(params);
    // query not used, but still here as example
    requestBuilder.url(CURRENT_URL);
    requestBuilder.tag(MEDIA_CALL);

    return fetchList(currentList, requestBuilder, filters, callback);
}

From source file:butter.droid.base.providers.media.VodoProvider.java

License:Open Source License

/**
 * Fetch the list of movies from YTS/*  www .  j  av  a  2  s . co  m*/
 *
 * @param currentList    Current shown list to be extended
 * @param requestBuilder Request to be executed
 * @param callback       Network callback
 * @return Call
 */
private Call fetchList(final ArrayList<Media> currentList, final Request.Builder requestBuilder,
        final Filters filters, final Callback callback) {
    return enqueue(requestBuilder.build(), new com.squareup.okhttp.Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            String url = requestBuilder.build().urlString();
            if (CURRENT_API >= API_URLS.length - 1) {
                callback.onFailure(e);
            } else {
                if (url.contains(API_URLS[CURRENT_API])) {
                    url = url.replace(API_URLS[CURRENT_API], API_URLS[CURRENT_API + 1]);
                    url = url.replace(API_URLS[CURRENT_API], API_URLS[CURRENT_API + 1]);
                    CURRENT_API++;
                } else {
                    url = url.replace(API_URLS[CURRENT_API - 1], API_URLS[CURRENT_API]);
                    url = url.replace(API_URLS[CURRENT_API - 1], API_URLS[CURRENT_API]);
                }
                requestBuilder.url(url);
                fetchList(currentList, requestBuilder, filters, callback);
            }
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                String responseStr;
                try {
                    responseStr = response.body().string();
                } catch (SocketException e) {
                    onFailure(response.request(), new IOException("Socket failed"));
                    return;
                }

                VodoResponse result;
                try {
                    result = mGson.fromJson(responseStr, VodoResponse.class);
                } catch (IllegalStateException e) {
                    onFailure(response.request(), new IOException("JSON Failed"));
                    return;
                } catch (JsonSyntaxException e) {
                    onFailure(response.request(), new IOException("JSON Failed"));
                    return;
                }

                if (result == null) {
                    callback.onFailure(new NetworkErrorException("No response"));
                } else if (result.downloads == null || result.downloads.size() <= 0) {
                    callback.onFailure(new NetworkErrorException("No movies found"));
                } else {
                    ArrayList<Media> formattedData = result.formatForApp(currentList);
                    callback.onSuccess(filters, formattedData, true);
                    return;
                }
            }
            onFailure(response.request(), new IOException("Couldn't connect to Vodo"));
        }
    });
}

From source file:butter.droid.base.providers.subs.YSubsProvider.java

License:Open Source License

@Override
public void getList(final Movie media, final Callback callback) {
    final Request.Builder requestBuilder = new Request.Builder();
    requestBuilder.url(API_URL + media.imdbId);
    requestBuilder.tag(SUBS_CALL);/*from ww w .  j  a  v a 2 s. c  om*/

    fetch(requestBuilder, media, new Callback() {
        @Override
        public void onSuccess(Map<String, String> items) {
            callback.onSuccess(items);
        }

        @Override
        public void onFailure(Exception e) {
            requestBuilder.url(MIRROR_URL + media.imdbId);
            fetch(requestBuilder, media, callback);
        }
    });
}

From source file:butter.droid.base.youtube.YouTubeData.java

License:Open Source License

/**
 * Calculate the YouTube URL to load the video.  Includes retrieving a token that YouTube
 * requires to play the video.//from  ww w. j a  v  a  2 s.c  o  m
 *
 * @param quality  quality of the video.  17=low, 18=high
 * @param fallback whether to fallback to lower quality in case the supplied quality is not available
 * @param videoId  the id of the video
 * @return the url string that will retrieve the video
 * @throws java.io.IOException
 */
public static String calculateYouTubeUrl(String quality, boolean fallback, String videoId) throws IOException {

    String uriStr = null;
    OkHttpClient client = ButterApplication.getHttpClient();

    Request.Builder request = new Request.Builder();
    request.url(YOUTUBE_VIDEO_INFORMATION_URL + videoId);
    Call call = client.newCall(request.build());
    Response response = call.execute();

    String infoStr = response.body().string();

    String[] args = infoStr.split("&");
    Map<String, String> argMap = new HashMap<String, String>();
    for (String arg : args) {
        String[] valStrArr = arg.split("=");
        if (valStrArr.length >= 2) {
            argMap.put(valStrArr[0], URLDecoder.decode(valStrArr[1]));
        }
    }

    //Find out the URI string from the parameters

    //Populate the list of formats for the video
    String fmtList = URLDecoder.decode(argMap.get("fmt_list"), "utf-8");
    ArrayList<Format> formats = new ArrayList<Format>();
    if (null != fmtList) {
        String formatStrs[] = fmtList.split(",");

        for (String lFormatStr : formatStrs) {
            Format format = new Format(lFormatStr);
            formats.add(format);
        }
    }

    //Populate the list of streams for the video
    String streamList = argMap.get("url_encoded_fmt_stream_map");
    if (null != streamList) {
        String streamStrs[] = streamList.split(",");
        ArrayList<VideoStream> streams = new ArrayList<VideoStream>();
        for (String streamStr : streamStrs) {
            VideoStream lStream = new VideoStream(streamStr);
            streams.add(lStream);
        }

        //Search for the given format in the list of video formats
        // if it is there, select the corresponding stream
        // otherwise if fallback is requested, check for next lower format
        int formatId = Integer.parseInt(quality);

        Format searchFormat = new Format(formatId);
        while (!formats.contains(searchFormat) && fallback) {
            int oldId = searchFormat.getId();
            int newId = getSupportedFallbackId(oldId);

            if (oldId == newId) {
                break;
            }
            searchFormat = new Format(newId);
        }

        int index = formats.indexOf(searchFormat);
        if (index >= 0) {
            VideoStream searchStream = streams.get(index);
            uriStr = searchStream.getUrl();
        }

    }
    //Return the URI string. It may be null if the format (or a fallback format if enabled)
    // is not found in the list of formats for the video
    return uriStr;
}