Example usage for com.squareup.okhttp Response isSuccessful

List of usage examples for com.squareup.okhttp Response isSuccessful

Introduction

In this page you can find the example usage for com.squareup.okhttp Response isSuccessful.

Prototype

public boolean isSuccessful() 

Source Link

Document

Returns true if the code is in [200..300), which means the request was successfully received, understood, and accepted.

Usage

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static boolean moviesWatchlist(List<MediumMovie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {//from w w w .  j a v  a 2  s  . co  m
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib
                .getJsonPostRequest((movies.get(0).toWatch() ? "http://api.trakt.tv/movie/watchlist/"
                        : "http://api.trakt.tv/movie/unwatchlist/") + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static boolean movieFavorite(List<Movie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {/*from   www  .  j  ava 2s. c  o m*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("imdb_id", movies.get(i).getImdbId());
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            jsonMovie.put("rating", movies.get(i).isFavourite() ? "love" : "unrate");
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/rate/movies/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static boolean moviesFavorite(List<MediumMovie> movies, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || movies.size() == 0)
        return false;

    try {/* w ww . j a va  2  s.  c o m*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = movies.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonMovie = new JSONObject();
            jsonMovie.put("tmdb_id", movies.get(i).getTmdbId());
            jsonMovie.put("year", movies.get(i).getReleaseYear());
            jsonMovie.put("title", movies.get(i).getTitle());
            jsonMovie.put("rating", movies.get(i).isFavourite() ? "love" : "unrate");
            array.put(jsonMovie);
        }
        json.put("movies", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/rate/movies/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static boolean addTvShowToLibrary(TraktTvShow show, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
        return false;

    try {/*from   w  ww  . j a  v a2s  .  co m*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);
        json.put("tvdb_id", show.getId());
        json.put("title", show.getTitle());

        JSONArray array = new JSONArray();
        for (String season : show.getSeasons().keySet()) {
            Collection<String> episodes = show.getSeasons().get(season);
            for (String episode : episodes) {
                JSONObject jsonShow = new JSONObject();
                jsonShow.put("season", season);
                jsonShow.put("episode", episode);
                array.put(jsonShow);
            }
        }
        json.put("episodes", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/show/episode/library/" + getApiKey(c),
                json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static boolean tvShowFavorite(List<TvShow> shows, Context c) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || shows.size() == 0)
        return false;

    try {/*w  ww .j  ava  2s . co m*/
        JSONObject json = new JSONObject();
        json.put("username", username);
        json.put("password", password);

        JSONArray array = new JSONArray();
        int count = shows.size();
        for (int i = 0; i < count; i++) {
            JSONObject jsonShow = new JSONObject();
            jsonShow.put("tvdb_id", shows.get(i).getId());
            jsonShow.put("title", shows.get(i).getTitle());
            jsonShow.put("rating", shows.get(i).isFavorite() ? "love" : "unrate");
            array.put(jsonShow);
        }
        json.put("shows", array);

        Request request = MizLib.getJsonPostRequest("http://api.trakt.tv/rate/shows/" + getApiKey(c), json);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        return response.isSuccessful();
    } catch (Exception e) {
        return false;
    }
}

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static JSONArray getMovieLibrary(Context c, int type) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
        return new JSONArray();

    try {/*from   w  w w .ja  v  a2 s .  c o m*/
        String url = "";
        if (type == WATCHED) {
            url = "http://api.trakt.tv/user/library/movies/watched.json/" + getApiKey(c) + "/" + username;
        } else if (type == RATINGS) {
            url = "http://api.trakt.tv/user/ratings/movies.json/" + getApiKey(c) + "/" + username + "/love";
        } else if (type == WATCHLIST) {
            url = "http://api.trakt.tv/user/watchlist/movies.json/" + getApiKey(c) + "/" + username;
        } else if (type == COLLECTION) {
            url = "http://api.trakt.tv/user/library/movies/collection.json/" + getApiKey(c) + "/" + username;
        }

        Request request = MizLib.getTraktAuthenticationRequest(url, username, password);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();

        if (response.isSuccessful())
            return new JSONArray(response.body().string());
        return new JSONArray();
    } catch (Exception e) {
        return new JSONArray();
    }
}

From source file:com.miz.apis.trakt.Trakt.java

License:Apache License

public static JSONArray getTvShowLibrary(Context c, int type) {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(c);
    String username = settings.getString(TRAKT_USERNAME, "").trim();
    String password = settings.getString(TRAKT_PASSWORD, "");

    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password))
        return new JSONArray();

    try {/*from ww  w  .j  a v a  2s. c om*/
        String url = "";
        if (type == WATCHED) {
            url = "http://api.trakt.tv/user/library/shows/watched.json/" + getApiKey(c) + "/" + username;
        } else if (type == RATINGS) {
            url = "http://api.trakt.tv/user/ratings/shows.json/" + getApiKey(c) + "/" + username + "/love";
        } else if (type == COLLECTION) {
            url = "http://api.trakt.tv/user/library/shows/collection.json/" + getApiKey(c) + "/" + username;
        }

        Request request = MizLib.getTraktAuthenticationRequest(url, username, password);
        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();

        if (response.isSuccessful())
            return new JSONArray(response.body().string());
        return new JSONArray();
    } catch (Exception e) {
        return new JSONArray();
    }
}

From source file:com.miz.functions.MizLib.java

License:Apache License

public static boolean downloadFile(String url, String savePath) {
    if (TextUtils.isEmpty(url))
        return false;

    InputStream in = null;/* w ww .ja v a 2s .  com*/
    OutputStream fileos = null;

    try {
        int bufferSize = 8192;
        byte[] retVal = null;

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

        Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
        if (!response.isSuccessful())
            return false;

        fileos = new BufferedOutputStream(new FileOutputStream(savePath));
        in = new BufferedInputStream(response.body().byteStream(), bufferSize);

        retVal = new byte[bufferSize];
        int length = 0;
        while ((length = in.read(retVal)) > -1) {
            fileos.write(retVal, 0, length);
        }
    } catch (Exception e) {
        // The download failed, so let's delete whatever was downloaded
        deleteFile(new File(savePath));

        return false;
    } finally {
        if (fileos != null) {
            try {
                fileos.flush();
                fileos.close();
            } catch (IOException e) {
            }
        }

        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }

    return true;
}

From source file:com.miz.service.MakeAvailableOffline.java

License:Apache License

private boolean beginTransfer() {
    if (mFileUrl.startsWith("http")) {

        InputStream in = null;//from  w  ww.  java 2s .co  m
        OutputStream fileos = null;

        try {
            int bufferSize = mBufferSize;
            byte[] retVal = null;

            Request request = new Request.Builder().url(mFileUrl).build();

            Response response = MizuuApplication.getOkHttpClient().newCall(request).execute();
            if (!response.isSuccessful())
                return false;

            File offline = FileUtils.getOfflineFile(mContext, mFileUrl);
            offline.createNewFile();

            fileos = new BufferedOutputStream(new FileOutputStream(offline));
            in = new BufferedInputStream(response.body().byteStream(), bufferSize);

            retVal = new byte[bufferSize];
            int length = 0;
            while ((length = in.read(retVal)) > -1) {
                if (mStopDownload) {
                    fileos.close();
                    in.close();
                    return false;
                }
                fileos.write(retVal, 0, length);
                mCurrentLength += length;
                update();
            }
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            if (fileos != null) {
                try {
                    fileos.flush();
                    fileos.close();
                } catch (IOException e) {
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    } else {
        try {
            File offline = FileUtils.getOfflineFile(mContext, mSmb.getCanonicalPath());
            offline.createNewFile();

            mTotalLength = mSmb.length();

            OutputStream os = new BufferedOutputStream(new FileOutputStream(offline), mBufferSize);
            BufferedInputStream in = new BufferedInputStream(new SmbFileInputStream(mSmb), mBufferSize);
            byte[] b = new byte[mBufferSize];
            int n;
            while ((n = in.read(b)) != -1) {
                if (mStopDownload) {
                    os.close();
                    in.close();
                    return false;
                }
                os.write(b, 0, n);
                mCurrentLength += n;
                update();
            }
            os.close();
            in.close();

            return true;
        } catch (Exception ignored) {
            return false;
        }
    }
}

From source file:com.mummyding.app.leisure.database.cache.cache.DailyCache.java

License:Open Source License

public void load() {
    Request.Builder builder = new Request.Builder();
    builder.url(DailyApi.daily_url);/*from   www.ja va2  s  . c  o m*/
    Request request = builder.build();
    HttpUtil.enqueue(request, new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful() == false) {
                mHandler.sendEmptyMessage(CONSTANT.ID_FAILURE);
                return;
            }
            String res = response.body().string();

            ArrayList<String> collectionTitles = new ArrayList<String>();
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).isCollected() == 1) {
                    collectionTitles.add(mList.get(i).getTitle());
                }
            }

            List<StoryBean> oldList = new ArrayList<StoryBean>();
            List<StoryBean> newList = new ArrayList<StoryBean>();

            for (StoryBean storyBean : mList) {
                oldList.add(storyBean);
            }

            Gson gson = new Gson();
            DailyBean dailyBean = gson.fromJson(res, DailyBean.class);
            StoryBean[] storyBeans = dailyBean.getStories();
            for (StoryBean storyBeen : storyBeans) {
                newList.add(storyBeen);
            }

            loadOld(dailyBean.getDate(), oldList, newList);

        }
    });
}