# Volley

In order to integrate Codavel SDK with Volley, we recommend using OkHttp (opens new window) as the HTTP engine.

Android minimum versions

  • SDK: 16
  • Gradle: 4.8.1

# Add repository

To add the Codavel SDK repository to your project, simply open the app or module's build.gradle and add the following block of code:

repositories {
  (...)
  maven { url 'https://artifact.codavel.com:8081/artifactory/android'}
  (...)
}
1
2
3
4
5

WARNING

Depending on your app organization, the Codavel SDK repository should be added differently. For example, your settings.gradle file can set specific dependency rules. Thus Codavel SDK repository needs to be added in settings.gradle file. Double-check that the repository and its dependencies were added correctly.

# Add SDK

Add the Codavel SDK and OkHttp as dependencies of the project and add the <APP_ID> and <APP_SECRET> to the app’s build.gradle script:

android {
  (...)
  defaultConfig {
    (...)
    resValue "string", "BOLINA_DEPLOY_ID", "<APP_ID>"
    resValue "string", "BOLINA_SECRET_ID", "<APP_SECRET>"
    (...)
  }
  (...)
}
dependencies {
  (...)
  implementation 'com.codavel.bolina:interceptor_okhttp3:0.10.36'
  implementation 'com.codavel.bolina:interceptor_volley:0.10.36'
  implementation 'com.squareup.okhttp3:okhttp:4.9.0'
  (...)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Enable other dependencies

To enable all functionalities of Codavel’s monitoring solution, Codavel SDK has the following dependencies:

  • In case you have not migrated your app to androidx library artifacts, include the Work Manager by adding the following block of code to the app’s build.gradle script:
dependencies {
    (...)
    implementation 'androidx.work:work-runtime:2.2.0'
    (...)
}
1
2
3
4
5

The Work Manager is used to dump relevant statistics on background jobs, thus reducing its impact in your app's behavior.

Additionally, add the following lines to your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true
1
2
  • Bolina also requires the following permissions, that should be added to your app’s manifest:
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
</manifest>
1
2
3
4

WARNING

If your app already defines a network security config, please check this instructions on how to add Codavel SDK support

# Start interceptor

Make sure that, before performing any HTTP/HTTPS request, you import and start the interceptor:

  • Initialize the Bolina Configuration and set the Bolina Configuration domain name:
  • Java
  • Kotlin
BolinaConfiguration config = new BolinaConfiguration();
config.setDomainName("<BOLINA_SERVER_URL>");
1
2
  • [optional] Configure your Origin:
  • Java
  • Kotlin
config.addBolinaURLReplacement("<your.cdn.com>", "<your.origin.com>");
1

With this configuration, all requests intercepted by Bolina that would originally be sent to <your.cdn.com>, will be sent to a Bolina Server, that then will fetch the resource from <your.origin.com>. In case the Bolina transfer fails, the requests will be sent to <your.cdn.com>, as before. You can add multiple entries to be replaced. Otherwise, without this configuration, Codavel Performance Service servers will perform the same request as your HTTP/HTTPS clients would, to your current CDN or Origin.

  • Start the Bolina interceptor:
  • Java
  • Kotlin
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {
    @Override
    public void run() {
        InterceptorSingleton.startInterceptor(getApplication(), config);
    }
});
1
2
3
4
5
6
7

WARNING

Check the interceptor lifecycle for more details on how to do it

  • Add Bolina OkHttpStack to Volley RequestQueue:
  • Java
  • Kotlin
RequestQueue queue = Volley.newRequestQueue(context, new BolinaOkHttpStack());
1
  • Perform the requests using the RequestQueue. For text-based requests, use the following example:
  • Java
  • Kotlin
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, null, new com.android.volley.Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            String x = "Response: " + response.toString();
            textView.setText(x);
        }
    }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            String x = "Error during request. Error message: " + error.toString();
            textView.setText(x);
        }
    });
queue.add(jsonObjectRequest);
queue.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

For image requests, use this following example:

  • Java
  • Kotlin
ImageRequest imageRequest = new ImageRequest(requestUrl, 
    new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            imageView.setImageBitmap(response);
        }
    },0,0, ImageView.ScaleType.CENTER_CROP,null, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            String x = "Error during request. Error message: " + error.toString();
            Log.e(TAG, x);
        }
    }
);

queue.add(imageRequest);
queue.start();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Stop interceptor

  • Make sure you stop the interceptor whenever you don’t need it:
  • Java
  • Kotlin
try {
    InterceptorSingleton.getInstance().stopInterceptor();
} catch (BolinaDeployException e) {
    // handle the exception
    e.printStackTrace();
}
1
2
3
4
5
6

WARNING

Check the interceptor lifecycle for more details on how to do it