fix: handle responses without content type (#101)

This fixes NullPointerException when response doesn't have content type. Exception stack trace:

```
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.MediaType.toString()' on a null object reference
        at io.socket.engineio.client.transports.PollingXHR$Request.onLoad(PollingXHR.java:271)
        at io.socket.engineio.client.transports.PollingXHR$Request.access$700(PollingXHR.java:148)
        at io.socket.engineio.client.transports.PollingXHR$Request$1.onResponse(PollingXHR.java:232)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:141)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
```
This commit is contained in:
Jacek Mleczek
2019-03-15 11:21:43 +01:00
committed by Damien Arrachequesne
parent 5c6519727f
commit 6f065b7a62

View File

@@ -268,10 +268,10 @@ public class PollingXHR extends Polling {
private void onLoad() {
ResponseBody body = response.body();
String contentType = body.contentType().toString();
MediaType mediaType = body.contentType();
try {
if (BINARY_CONTENT_TYPE.equalsIgnoreCase(contentType)) {
if (mediaType != null && BINARY_CONTENT_TYPE.equalsIgnoreCase(mediaType.toString())) {
this.onData(body.bytes());
} else {
this.onData(body.string());