Example usage for com.squareup.okhttp OkHttpClient OkHttpClient

List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient

Introduction

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

Prototype

public OkHttpClient() 

Source Link

Usage

From source file:com.raxdenstudios.http.utils.OkHttpStack.java

License:Open Source License

public OkHttpStack() {
    this(new OkUrlFactory(new OkHttpClient()));
}

From source file:com.ringcentral.rc_android_sdk.rcsdk.http.Client.java

License:Open Source License

public Client() {
        client = new OkHttpClient();
    }

From source file:com.rowland.movies.ApplicationController.java

License:Apache License

public static Retrofit getRetrofit() {
    // Set custom date format for Gson
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    SessionRequestInterceptor sessionRequestInterceptor = new SessionRequestInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    // Our client
    OkHttpClient client = new OkHttpClient();
    // Set other interceptors
    client.networkInterceptors().add(new StethoInterceptor());
    // Set SessionRequestInterceptor instance as interceptor
    client.interceptors().add(sessionRequestInterceptor);
    // Set HttpLoggingInterceptor instance as last interceptor
    client.interceptors().add(logging);/*from   w w  w. j a va2s .  c  o m*/
    //To send out network requests to an API_MOVIE_URL, we need to use the Retrofit builder class
    Retrofit retrofit = new Retrofit.Builder().baseUrl(EBaseURlTypes.MOVIE_API_BASE_URL.getUrlType())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).build();
    // Wollah! Retrofit instance is served hot.
    return retrofit;
}

From source file:com.rowland.moviesquire.ApplicationController.java

License:Apache License

public static Retrofit getRetrofit() {
    // Set custom date format for Gson
    Gson gson = new GsonBuilder()
            .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
            .excludeFieldsWithoutExposeAnnotation().setDateFormat("yyyy-MM-dd").create();

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    SessionRequestInterceptor sessionRequestInterceptor = new SessionRequestInterceptor();
    // set your desired log level
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    // Our client
    OkHttpClient client = new OkHttpClient();
    // Set other interceptors
    client.networkInterceptors().add(new StethoInterceptor());
    // Set SessionRequestInterceptor instance as interceptor
    client.interceptors().add(sessionRequestInterceptor);
    // Set HttpLoggingInterceptor instance as last interceptor
    client.interceptors().add(logging);/* w  w  w  .java  2s . c  om*/
    //To send out network requests to an API_MOVIE_URL, we need to use the Retrofit builder class
    Retrofit retrofit = new Retrofit.Builder().baseUrl(EBaseURlTypes.MOVIE_API_BASE_URL.getUrlType())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(client).build();
    // Wollah! Retrofit instance is served hot.
    return retrofit;
}

From source file:com.rsu.nuttanun.testdrivingvlicense.ConfirmScoreActivity.java

public void clickOKConfirm(View view) {

    String urlPHP = "http://swiftcodingthai.com/toey/add_score.php";

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0])
            .add("Date", dateString).add("Score", scoreString).build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override//from  w ww.ja va  2 s  .co  m
        public void onFailure(Request request, IOException e) {

        }

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

            Intent intent = new Intent(ConfirmScoreActivity.this, ScoreListView.class);
            intent.putExtra("login", loginStrings);
            startActivity(intent);
            finish();
        }
    });

}

From source file:com.rsu.nuttanun.testdrivingvlicense.ScoreListView.java

private void createListView() {

    String urlPHP = "http://swiftcodingthai.com/toey/get_score_where.php";
    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("id_login", loginStrings[0])
            .build();/*from www  . j  a v  a  2  s .  com*/
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

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

            String strResponse = response.body().string();
            Log.d("10AugV2", "strResponse ==>" + strResponse);

            try {

                JSONArray jsonArray = new JSONArray(strResponse);

                String[] dateStrings = new String[jsonArray.length()];
                String[] scoreStrings = new String[jsonArray.length()];

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    dateStrings[i] = jsonObject.getString("Date");
                    scoreStrings[i] = jsonObject.getString("Score") + " ?";

                } //for

                CoaurseAdapter coaurseAdapter = new CoaurseAdapter(ScoreListView.this, 1, dateStrings,
                        scoreStrings);
                listView.setAdapter(coaurseAdapter);

            } catch (Exception e) {
                e.printStackTrace();

            }

        } // onRespose
    });

}

From source file:com.rsu.nuttanun.testdrivingvlicense.SignUpActivity.java

private void upLoadNewUser() {

    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormEncodingBuilder().add("isAdd", "true").add("Name", nameString)
            .add("Surname", surnameString).add("Age", ageString).add("User", userString)
            .add("Password", passwordString).build();
    Request.Builder builder = new Request.Builder();
    Request request = builder.url(urlPHP).post(requestBody).build();
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override//from   w  w  w . ja  v a2s.c  o m
        public void onFailure(Request request, IOException e) {

        }

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

        }
    });

}

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";
    }//  w w w. ja  v a  2 s  .  c  om

    //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.sam_chordas.android.stockhawk.app.retofit.RestClientPublic.java

License:Apache License

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.interceptors().add(interceptor);

    client.networkInterceptors().add(new Interceptor() {
        @Override/*w ww.  j  ava  2s . c  o  m*/
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            HttpUrl url = request.httpUrl().newBuilder()
                    .addQueryParameter(App.getGlobalContext().getString(R.string.url_query_format),
                            App.getGlobalContext().getString(R.string.url_query_json))
                    .addQueryParameter(App.getGlobalContext().getString(R.string.url_query_diagnostics),
                            App.getGlobalContext().getString(R.string.url_query_diagnostics_value))
                    .addQueryParameter(App.getGlobalContext().getString(R.string.url_query_evn),
                            App.getGlobalContext().getString(R.string.url_query_evn_value))
                    .build();
            request = request.newBuilder().url(url).build();
            return chain.proceed(request);

        }
    });
    return client;
}

From source file:com.sam_chordas.android.stockhawk.service.HistoricData.HistoricData.java

License:Apache License

private String fetchData(String url) throws IOException {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();

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

    return response.body().string();
}