List of usage examples for com.squareup.okhttp Response isSuccessful
public boolean isSuccessful()
From source file:api.QueryManager.java
License:Open Source License
private Reader executeRequest(Request request, boolean limit) { // Get response Response response = null; try {/*from w ww .j a va 2s . c om*/ if (limit) { // Obey the rate limit longRateLimiter.acquire(); shortRateLimiter.acquire(); } response = client.newCall(request).execute(); } catch (IOException e) { // Handle error throw new UltiException(UltiException.Type.UNKNOWN); } // Handle any errors if (!response.isSuccessful()) { handleErrorCode(response.code()); } return response.body().charStream(); }
From source file:apijson.demo.client.manager.HttpManager.java
License:Apache License
/** * @param client// w w w.j av a 2 s. co m * @param request * @return * @throws Exception */ private String getResponseJson(OkHttpClient client, Request request) throws Exception { if (client == null || request == null) { Log.e(TAG, "getResponseJson client == null || request == null >> return null;"); return null; } Response response = client.newCall(request).execute(); return response.isSuccessful() ? response.body().string() : null; }
From source file:at.aau.itec.android.mediaplayer.dash.DashParser.java
License:Open Source License
/** * Parses an MPD XML file. This needs to be executed off the main thread, else a * NetworkOnMainThreadException gets thrown. * @param source the URl of an MPD XML file * @return a MPD object// w w w .j a va2s . c om * @throws android.os.NetworkOnMainThreadException if executed on the main thread */ public MPD parse(UriSource source) { MPD mpd = null; OkHttpClient httpClient = new OkHttpClient(); Headers.Builder headers = new Headers.Builder(); if (source.getHeaders() != null && !source.getHeaders().isEmpty()) { for (String name : source.getHeaders().keySet()) { headers.add(name, source.getHeaders().get(name)); } } Request.Builder request = new Request.Builder().url(source.getUri().toString()).headers(headers.build()); try { Response response = httpClient.newCall(request.build()).execute(); if (!response.isSuccessful()) { throw new IOException("error requesting the MPD"); } mpd = parse(response.body().byteStream()); } catch (IOException e) { Log.e(TAG, "error downloading the MPD", e); throw new RuntimeException("error downloading the MPD", e); } catch (XmlPullParserException e) { Log.e(TAG, "error parsing the MPD", e); throw new RuntimeException("error parsing the MPD", e); } return mpd; }
From source file:butter.droid.base.providers.media.AnimeProvider.java
License:Open Source License
/** * Fetch the list of movies from Haruhichan * * @param currentList Current shown list to be extended * @param requestBuilder Request to be executed * @param callback Network callback * @return Call/*w w w. j ava 2 s. c om*/ */ 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); } AnimeResponse result = new AnimeResponse(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 AnimeAPI")); } }); }
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);// w w w.java 2s. c o m requestBuilder.tag(MEDIA_CALL); 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
/** * Fetch the list of movies from API//from w ww .j a v a 2 s . c om * * @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
/** * Fetch the list of movies from EZTV/*w ww .j a v a2s.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]); 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);/*from w w w . ja va 2s.com*/ requestBuilder.tag(MEDIA_CALL); 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
/** * Fetch the list of movies from YTS/* w ww. ja v a 2 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]); 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.SubsProvider.java
License:Open Source License
/** * @param context Context//from ww w . ja v a 2s .co m * @param media Media data * @param languageCode Code of language * @param callback Network callback * @return Call */ public static Call download(final Context context, final Media media, final String languageCode, final com.squareup.okhttp.Callback callback) { OkHttpClient client = ButterApplication.getHttpClient(); if (media.subtitles != null && media.subtitles.containsKey(languageCode)) { try { Request request = new Request.Builder().url(media.subtitles.get(languageCode)).build(); Call call = client.newCall(request); final File subsDirectory = getStorageLocation(context); final String fileName = media.videoId + "-" + languageCode; final File srtPath = new File(subsDirectory, fileName + ".srt"); if (srtPath.exists()) { callback.onResponse(null); return call; } call.enqueue(new com.squareup.okhttp.Callback() { @Override public void onFailure(Request request, IOException e) { callback.onFailure(request, e); } @Override public void onResponse(Response response) throws IOException { if (response.isSuccessful()) { InputStream inputStream = null; boolean failure = false; try { subsDirectory.mkdirs(); if (srtPath.exists()) { File to = new File(subsDirectory, "temp" + System.currentTimeMillis()); srtPath.renameTo(to); to.delete(); } inputStream = response.body().byteStream(); String urlString = response.request().urlString(); if (urlString.contains(".zip") || urlString.contains(".gz")) { SubsProvider.unpack(inputStream, srtPath, languageCode); } else if (SubsProvider.isSubFormat(urlString)) { parseFormatAndSave(urlString, srtPath, languageCode, inputStream); } else { callback.onFailure(response.request(), new IOException("FatalParsingException")); failure = true; } } catch (FatalParsingException e) { e.printStackTrace(); callback.onFailure(response.request(), new IOException("FatalParsingException")); failure = true; } catch (FileNotFoundException e) { e.printStackTrace(); callback.onFailure(response.request(), e); failure = true; } catch (IOException e) { e.printStackTrace(); callback.onFailure(response.request(), e); failure = true; } finally { if (inputStream != null) inputStream.close(); if (!failure) callback.onResponse(response); } } else { callback.onFailure(response.request(), new IOException("Unknown error")); } } }); return call; } catch (RuntimeException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } callback.onFailure(null, new IOException("Wrong media")); return null; }