Gleap
Search…
🌏
Network logs
Gleap allows you to monitor all network requests for a better understanding of what's happening within your apps. It can be done manually by passing the data to Gleap or if you're using OkHttp with the Gleap OkHttpInterceptor.

Logging HttpUrlConnection requests

There are several ways to log the network with HttpUrlConnection. The recommended way is to use the following method. All information from the connection is automatically gathered for you. The requestBody and result can be passed on as JSON or String.
1
Gleap.getInstance().logNetwork((HttpsURLConnection) conn, requestBody, result);
Copied!
​
Let's have a more detailed example. This request posts data to an endpoint.
1
....
2
@Override
3
protected Object doInBackground(Object[] objects) {
4
HttpURLConnection conn = null;
5
JSONObject result = null;
6
try {
7
URL url = new URL("YOUR_URL");
8
conn = (HttpURLConnection) url.openConnection();
9
conn.setRequestProperty("Accept", "application/json");
10
conn.setRequestProperty("Content-Type", "application/json");
11
conn.setRequestMethod("POST");
12
​
13
JSONObject requestBody = new JSONObject();
14
try {
15
requestBody.put("Key", "Value");
16
requestBody.put("Key2", "Value");
17
} catch (JSONException e) {
18
e.printStackTrace();
19
}
20
​
21
​
22
try (OutputStream os = conn.getOutputStream()) {
23
byte[] input = requestBody.toString().getBytes(StandardCharsets.UTF_8);
24
os.write(input, 0, input.length);
25
}
26
​
27
try (BufferedReader br = new BufferedReader(
28
new InputStreamReader(conn.getInputStream(), "utf-8"))) {
29
​
30
String input;
31
while ((input = br.readLine()) != null) {
32
result = new JSONObject(input);
33
}
34
} catch (JSONException e) {
35
e.printStackTrace();
36
}
37
Gleap.getInstance().logNetwork((HttpsURLConnection) conn, requestBody, result);
38
} catch(Exception e) {
39
e.printStackTrace();
40
}
Copied!

OkHttp

There is an easy way to intercept all requests from OkHttp. This will be logged automatically by Gleap.
First of all, import the OkHttp interceptor for Gleap in your build.gradle(root).
1
implementation group: 'io.gleap', name: 'gleap-okhttp-interceptor', version: '6.3.0'
Copied!
The interceptor can be used like this:
1
OkHttpClient client = new OkHttpClient.Builder()
2
.addInterceptor(new GleapOkHttpInterceptor())
3
.build();
4
​
5
Request request = new Request.Builder()
6
.url("http://www.publicobject.com/helloworld.txt")
7
.header("User-Agent", "OkHttp Example")
8
.build();
9
​
10
Response response = client.newCall(request).execute();
11
response.body().close();
Copied!

Other Libraries

If you are using any other library than HTTPUrlConnection or OkHttp you can manually log any requests with the following method.
1
void logNetwork("YOUR_URL", RequestType.GET, 200, 150, requestBody, response);
Copied!
​
Last modified 6d ago