Last active
February 8, 2020 07:46
-
-
Save esabook/b5fa756c9a5ef3fc3e0489549f9565d1 to your computer and use it in GitHub Desktop.
connection listener for okhttp-retrofit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ***.utils; | |
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.Proxy; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import javax.annotation.Nullable; | |
import okhttp3.Call; | |
import okhttp3.Connection; | |
import okhttp3.EventListener; | |
import okhttp3.Handshake; | |
import okhttp3.Protocol; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import static ***.utils.__Utils.trackEvent; | |
public class __ConnectionEvent extends EventListener { | |
void tractEvent(Call call, String eventName) { | |
Map<String, String> props = new HashMap<>(); | |
props.put("Endpoint", call.request().url().encodedPath().toString()); | |
trackEvent("NETWORK EVENT", props); | |
} | |
@Override | |
public void callStart(Call call) { | |
super.callStart(call); | |
tractEvent(call, "callStart"); | |
} | |
@Override | |
public void dnsStart(Call call, String domainName) { | |
super.dnsStart(call, domainName); | |
tractEvent(call, "dnsStart"); | |
} | |
@Override | |
public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) { | |
super.dnsEnd(call, domainName, inetAddressList); | |
tractEvent(call, "dnsEnd"); | |
} | |
@Override | |
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) { | |
super.connectStart(call, inetSocketAddress, proxy); | |
tractEvent(call, "connectStart"); | |
} | |
@Override | |
public void secureConnectStart(Call call) { | |
super.secureConnectStart(call); | |
tractEvent(call, "secureConnectStart"); | |
} | |
@Override | |
public void secureConnectEnd(Call call, @Nullable Handshake handshake) { | |
super.secureConnectEnd(call, handshake); | |
tractEvent(call, "secureConnectEnd"); | |
} | |
@Override | |
public void connectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, @Nullable Protocol protocol) { | |
super.connectEnd(call, inetSocketAddress, proxy, protocol); | |
tractEvent(call, "connectEnd"); | |
} | |
@Override | |
public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, @Nullable Protocol protocol, IOException ioe) { | |
super.connectFailed(call, inetSocketAddress, proxy, protocol, ioe); | |
tractEvent(call, "connectFailed"); | |
} | |
@Override | |
public void connectionAcquired(Call call, Connection connection) { | |
super.connectionAcquired(call, connection); | |
tractEvent(call, "connectionAcquired"); | |
} | |
@Override | |
public void connectionReleased(Call call, Connection connection) { | |
super.connectionReleased(call, connection); | |
tractEvent(call, "connectionReleased"); | |
} | |
@Override | |
public void requestHeadersStart(Call call) { | |
super.requestHeadersStart(call); | |
tractEvent(call, "requestHeadersStart"); | |
} | |
@Override | |
public void requestHeadersEnd(Call call, Request request) { | |
super.requestHeadersEnd(call, request); | |
tractEvent(call, "requestHeadersEnd"); | |
} | |
@Override | |
public void requestBodyStart(Call call) { | |
super.requestBodyStart(call); | |
tractEvent(call, "requestBodyStart"); | |
} | |
@Override | |
public void requestBodyEnd(Call call, long byteCount) { | |
super.requestBodyEnd(call, byteCount); | |
tractEvent(call, "requestBodyEnd"); | |
} | |
@Override | |
public void responseHeadersStart(Call call) { | |
super.responseHeadersStart(call); | |
tractEvent(call, "responseHeadersStart"); | |
} | |
@Override | |
public void responseHeadersEnd(Call call, Response response) { | |
super.responseHeadersEnd(call, response); | |
tractEvent(call, "responseHeadersEnd"); | |
} | |
@Override | |
public void responseBodyStart(Call call) { | |
super.responseBodyStart(call); | |
tractEvent(call, "responseBodyStart"); | |
} | |
@Override | |
public void responseBodyEnd(Call call, long byteCount) { | |
super.responseBodyEnd(call, byteCount); | |
tractEvent(call, "responseBodyEnd"); | |
} | |
@Override | |
public void callEnd(Call call) { | |
super.callEnd(call); | |
tractEvent(call, "callEnd"); | |
} | |
@Override | |
public void callFailed(Call call, IOException ioe) { | |
super.callFailed(call, ioe); | |
tractEvent(call, "callFailed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment