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.quarterfull.newsAndroid.reader.HttpJsonRequest.java

License:Open Source License

public InputStream PerformJsonRequest(HttpUrl url) throws Exception {
    Request request = new Request.Builder().url(url).get().build();

    //http://nelenkov.blogspot.de/2011/12/using-custom-certificate-trust-store-on.html
    //http://stackoverflow.com/questions/5947162/https-and-self-signed-certificate-issue
    //http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e537
    //http://stackoverflow.com/questions/859111/how-do-i-accept-a-self-signed-certificate-with-a-java-httpsurlconnection
    //http://developer.android.com/training/articles/security-ssl.html

    // CookieHandler.setDefault(new CookieManager());

    Response response = client.newCall(request).execute();

    if (response.isSuccessful()) {
        return response.body().byteStream();
    } else {/*  www  .  j  av  a  2s .c o  m*/
        throw new Exception(response.message());
    }
}

From source file:com.quwu.xinwo.home_page.City_WideActivity.java

/**
 * ?/*from  ww w  .java 2s.  c om*/
 * */
public String firstDoPost(String url, String parameter) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("city_region", parameter).add("small_area", "0")
            .add("twolevel_id", "1").add("three_id", "15").add("sortorder_id", "1").add("goods_lprice", "0")
            .add("goods_hprice", "800000").add("state", "1").add("pageNow", String.valueOf(pageNow))
            .add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {

        e.printStackTrace();
    } // execute

    return null;
}

From source file:com.quwu.xinwo.home_page.City_WideActivity.java

/**
 * ???/*from  w  ww  . ja  va  2s .  c om*/
 * */
public String MyDoPost(String url, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String city_region,
        String small_area, String twolevel_id, String three_id, String sortorder_id, String goods_lprice,
        String goods_hprice, String fristlevel, String buy_userid) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("normal_use", normal_use)
            .add("warranty_period", warranty_period).add("no_repair", no_repair).add("brand_new", brand_new)
            .add("jingdong", jingdong).add("mainland_licensed", mainland_licensed).add("since", since)
            .add("city_region", city_region).add("small_area", small_area).add("twolevel_id", twolevel_id)
            .add("three_id", three_id).add("sortorder_id", sortorder_id).add("goods_lprice", goods_lprice)
            .add("goods_hprice", goods_hprice).add("fristlevel", fristlevel).add("buy_userid", buy_userid)
            .add("pageNow", String.valueOf(pageNow)).add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {

        e.printStackTrace();
    } // execute

    return null;
}

From source file:com.quwu.xinwo.home_page.Crowd_FundingActivity.java

/**
 * ???//from   ww w.ja va  2  s  . co  m
 * */
public static String MyDoPost(String url, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String good_region,
        String city_region, String small_area, String twolevel_id, String three_id, String sortorder_id,
        String goods_lprice, String goods_hprice, String buy_userid, int pageNow, int pageSize) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("normal_use", normal_use)
            .add("warranty_period", warranty_period).add("no_repair", no_repair).add("brand_new", brand_new)
            .add("jingdong", jingdong).add("mainland_licensed", mainland_licensed).add("since", since)
            .add("good_region", good_region).add("city_region", city_region).add("small_area", small_area)
            .add("twolevel_id", twolevel_id).add("three_id", three_id).add("sortorder_id", sortorder_id)
            .add("goods_lprice", goods_lprice).add("goods_hprice", goods_hprice).add("buy_userid", buy_userid)
            .add("pageNow", String.valueOf(pageNow)).add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {

        e.printStackTrace();
    } // execute

    return null;
}

From source file:com.quwu.xinwo.home_page.DigitalActivity.java

/**
 * ???/*w ww .j  a  v  a 2 s . com*/
 * */
public static String MyDoPost(String url, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String good_region,
        String city_region, String small_area, String twolevel_id, String three_id, String sortorder_id,
        String goods_lprice, String goods_hprice, String buy_userid, int pageNow, int pageSize) {
    OkHttpClient mOkHttpClient = new OkHttpClient();
    RequestBody formBody = new FormEncodingBuilder().add("normal_use", normal_use)
            .add("warranty_period", warranty_period).add("no_repair", no_repair).add("brand_new", brand_new)
            .add("jingdong", jingdong).add("mainland_licensed", mainland_licensed).add("since", since)
            .add("good_region", good_region).add("city_region", city_region).add("small_area", small_area)
            .add("twolevel_id", twolevel_id).add("three_id", three_id).add("sortorder_id", sortorder_id)
            .add("goods_lprice", goods_lprice).add("goods_hprice", goods_hprice).add("buy_userid", buy_userid)
            .add("pageNow", String.valueOf(pageNow)).add("pageSize", String.valueOf(pageSize)).build();

    //  header(name, value)
    // ?name?value?? addHeader(name, value)
    // ??
    Request request = new Request.Builder().url(url).header("User-Agent", "OkHttp Headers.java")
            .addHeader("Accept", "application/json; q=0.5")
            .addHeader("Accept", "application/vnd.github.v3+json").post(formBody).build();
    Response response;
    try {
        response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String body = response.body().string();
            return body;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } // execute
    return null;
}

From source file:com.saiyanstudio.groceryassistant.RecipeActivity.java

private void initializeData() {

    //food2fork api
    //spoonacularFridgeAPIURL = "http://food2fork.com/api/search?key=eae722f5d2be7fd8fb1a0cd6a9310dbd&q=shredded%20chicken";

    spoonacularFridgeAPIURL = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/findByIngredients?ingredients=";

    String ingredientListString = "";
    for (int j = 0; j < ingredientList.size(); j++) {
        ingredientListString = ingredientListString + ingredientList.get(j).toLowerCase().replace(" ", "+");
        if (j != (ingredientList.size() - 1))
            ingredientListString += "%2C";
    }/*  www .j  av a2s  .c o  m*/

    //ingredientListString = "apples%2Cflour%2Csugar";

    spoonacularFridgeAPIURL += ingredientListString;

    spoonacularFridgeAPIURL += "&limitLicense=false&number=10&ranking=1";

    Log.i("spoonacularFridgeAPIURL", spoonacularFridgeAPIURL);

    if (isNetworkAvailable()) {

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(spoonacularFridgeAPIURL)
                .addHeader("X-Mashape-Key", "SDS3P5crHImshhb4jdBQayo4b77hp1TvcMgjsnNMrYbE6MZuYI")
                .addHeader("Accept", "application/json").build();

        /*
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
            .url(spoonacularFridgeAPIURL)
            .build();
        */

        Call call = client.newCall(request);
        call.enqueue(new Callback() {

            @Override
            public void onFailure(Request request, IOException e) {
            }

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

                RecipeActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recipeProgressBar.setVisibility(View.GONE);
                    }
                });

                try {
                    recipeJsonData = response.body().string();
                    Log.v("RECIPE_ACTIVITY", recipeJsonData);

                    if (response.isSuccessful()) {

                        JSONArray recipeArray = new JSONArray(recipeJsonData);
                        Log.d("RECIPE_ACTIVITY", "recipeArray.length() : " + recipeArray.length());
                        for (int i = 0; i < recipeArray.length(); i++) {
                            JSONObject recipeData = recipeArray.getJSONObject(i);
                            Log.d("RECIPE_ACTIVITY", "Recipe Name : " + recipeData.getString("title"));

                            RecipeItem recipeItem = new RecipeItem(recipeData.getString("title"),
                                    recipeData.getString("image"), recipeData.getInt("likes"),
                                    recipeData.getInt("usedIngredientCount"),
                                    recipeData.getInt("usedIngredientCount"));
                            //RecipeItem recipeItem = new RecipeItem(recipeData.getString("title"),recipeData.getString("image_url"),3,(int)recipeData.getDouble("social_rank"));

                            recipeItems.add(recipeItem);
                        }

                        RecipeActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                if (recipeItems.size() == 0)
                                    noRecipeMsg.setVisibility(View.VISIBLE);
                                else
                                    noRecipeMsg.setVisibility(View.INVISIBLE);

                                adapter.notifyDataSetChanged();
                            }
                        });
                    } else {

                        RecipeActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                noRecipeMsg.setVisibility(View.VISIBLE);
                                //Toast.makeText(RecipeActivity.this, "Sorry no recipes available", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                } catch (IOException e) {
                    Log.e("RECIPE_ACTIVITY", "Exception caught: ", e);
                } catch (JSONException e) {
                    Log.e("RECIPE_ACTIVITY", "Exception caught: ", e);
                }

            }
        });
    } else {
        Snackbar.make(linearLayout_recipe, "Network Unavialable", Snackbar.LENGTH_INDEFINITE).show();
        //Toast.makeText(RecipeActivity.this,"Network Unavialable", Toast.LENGTH_SHORT).show();
    }
}

From source file:com.stockbrowser.search.OpenSearchSearchEngine.java

License:Apache License

/**
 * Executes a GET request and returns the response content.
 *
 * @param urlString Request URI./*  w w  w .j  a  v a 2 s .c om*/
 * @return The response content. This is the empty string if the response contained no content. <p/>
 * OkHttp?HTTP
 */
public String readUrl(String urlString) {
    //        OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(urlString).build();
    try {
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().toString();
        } else {
            Log.i(TAG, "Suggestion request failed");
            return null;
        }
    } catch (IOException e) {
        Log.w(TAG, "Error", e);
        return null;
    }
}

From source file:com.tanmay.blip.networking.UpdateCheckService.java

License:Apache License

@Override
protected void onHandleIntent(Intent intent) {
    DatabaseManager databaseManager = new DatabaseManager(this);
    Gson gson = new Gson();
    Request request = new Request.Builder().url("http://xkcd.com/info.0.json").build();
    try {//from   w ww  .  jav  a 2 s .  c o m
        Response response = BlipApplication.getInstance().client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException();
        String responseBody = response.body().string();
        if (responseBody.substring(0, 1).equals("{")) {
            Comic comic = gson.fromJson(responseBody, Comic.class);
            if (!databaseManager.comicExists(comic)) {
                databaseManager.addComic(comic);
                Intent notifIntent = new Intent(NEW_COMIC);
                notifIntent.putExtra(EXTRA_NUM, comic.getNum());
                sendBroadcast(notifIntent);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.tanmay.blip.networking.XKCDDownloader.java

License:Apache License

private void downloadSpecific(int i) {
    Gson gson = new Gson();
    DatabaseManager databaseManager = new DatabaseManager(this);
    String url = String.format(COMICS_URL, i);
    Request request = new Request.Builder().url(url).build();
    try {/*  w w w .  j a  v a 2s.c  om*/
        Response response = BlipApplication.getInstance().client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException();
        Comic comic = gson.fromJson(response.body().string(), Comic.class);
        databaseManager.addComic(comic);
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DOWNLOAD_SUCCESS));
    } catch (IOException e) {
        e.printStackTrace();
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DOWNLOAD_FAIL));
    }
}

From source file:com.tanmay.blip.networking.XKCDDownloader.java

License:Apache License

private void downloadToday() {
    DatabaseManager databaseManager = new DatabaseManager(this);
    Gson gson = new Gson();
    Request request = new Request.Builder().url(LATEST_URL).build();
    try {//w ww . ja v  a  2 s . c  o  m
        Response response = BlipApplication.getInstance().client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException();
        Comic comic = gson.fromJson(response.body().string(), Comic.class);
        if (!databaseManager.comicExists(comic))
            databaseManager.addComic(comic);
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DOWNLOAD_SUCCESS));
    } catch (IOException e) {
        e.printStackTrace();
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(DOWNLOAD_FAIL));
    }
}