baasbox / Android-SDK

BaasBox Android SDK
Apache License 2.0
27 stars 26 forks source link

ProtocolException when calling plugin #32

Closed dirkam closed 9 years ago

dirkam commented 9 years ago

I'm using 0.9.0 server with 0.9.0 Android SDK.

I call my plugins with: BaasBox box = BaasBox.getDefault(); box.rest(HttpRequest.POST, "plugin/my.plugin", new JsonObject().put("my", "data"), true, new BaasHandler() { @Override public void handle(BaasResult res) { } });

Most of the time it works fine, however, sometimes I get unhandled Exception:

01-11 20:18:16.295 I/BAASBOX﹕ requested {url ->http://x.x.x.x:x/plugin/my.plugin method: POST, headers ->{...}}

01-11 20:18:16.301 25569-25595/my.package E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-5894 Process: my.package, PID: 25569 java.lang.Error: Got a protocol exception while setting http method at com.baasbox.android.HttpUrlConnectionClient.setupConnectionForRequest(HttpUrlConnectionClient.java:240) at com.baasbox.android.HttpUrlConnectionClient.execute(HttpUrlConnectionClient.java:154) at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:151) at com.baasbox.android.NetworkTask.parseResponse(NetworkTask.java:74) at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:152) at com.baasbox.android.impl.Task.execute(Task.java:189) at com.baasbox.android.impl.Dispatcher$Worker.run(Dispatcher.java:170) Caused by: java.net.ProtocolException: content-length promised 7 bytes, but received 0 at com.android.okhttp.internal.http.RetryableSink.close(RetryableSink.java:50) at com.android.okio.RealBufferedSink.close(RealBufferedSink.java:174) at com.android.okio.RealBufferedSink$1.close(RealBufferedSink.java:142) at com.baasbox.android.HttpUrlConnectionClient.copyStream(HttpUrlConnectionClient.java:261) at com.baasbox.android.HttpUrlConnectionClient.addBody(HttpUrlConnectionClient.java:248) at com.baasbox.android.HttpUrlConnectionClient.setupConnectionForRequest(HttpUrlConnectionClient.java:224)             at com.baasbox.android.HttpUrlConnectionClient.execute(HttpUrlConnectionClient.java:154)             at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:151)             at com.baasbox.android.NetworkTask.parseResponse(NetworkTask.java:74)             at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:152)             at com.baasbox.android.impl.Task.execute(Task.java:189)             at com.baasbox.android.impl.Dispatcher$Worker.run(Dispatcher.java:170)

giastfader commented 9 years ago

What if you call the plugin with same parameters and body via REST using for example Postman?

dirkam commented 9 years ago

I didn't have any issues using Postman.

giastfader commented 9 years ago

What are the headers and the body sent by the server? You can see them in Postman in body and headers sections

dirkam commented 9 years ago

Headers: Access-Control-Allow-Headers →X-Requested-With Access-Control-Allow-Origin →* BB-USERNAME →myuser Content-Length →2986 Content-Type →application/json; charset=utf-8

Body: { "result": "ok", "data": { "myjsonarray": [ { ... } ] }, "http_code": 200 }

eliantor commented 9 years ago

Hi,

are you using OkHttp client?

dirkam commented 9 years ago

Hi,

I'm using BaasBox SDK to call the endpoint.

BaasBox box = BaasBox.getDefault(); box.rest(HttpRequest.POST, "plugin/my.plugin", new JsonObject().put("my", "data"), true, new BaasHandler() { @Override public void handle(BaasResult res) { } });

eliantor commented 9 years ago

Sure, I've maybe formulated the question in a wrong way. From the stack trace it seems that you are setting the RestClient to OkClient on BaasBox initialization. Am I right?

dirkam commented 9 years ago

Sorry for the noob question, but how can I check this?

eliantor commented 9 years ago

Not a so noob question i guess, some docs are still missing. Anyway, do you do anything like the second line of code below during initialization of the library?

BaasBox.builder(getContext())
              .setRestClient(new OkClient())
              .init();

Just checking to see where the bug could be.

dirkam commented 9 years ago

No, I don't. This is what I use based on the docs:

BaasBox.Builder b = new BaasBox.Builder(this); client = b.setApiDomain(APIDOMAIN) .setAppCode(APPCODE).setPort(HTTPORT) .setAuthentication(BaasBox.Config.AuthType.SESSION_TOKEN) .init();

eliantor commented 9 years ago

Thanks,

just another quick question, to ease dissecting the bug, which android version are you running on?

dirkam commented 9 years ago

Nexus 4, stock 5.0.1. Haven't tried it on another device yet.

Btw, I'm happy to try with OkClient if it makes sense.

eliantor commented 9 years ago

You are indeed already using part of it. It's bundled in the platform since lollipop. I'll check and let you know asap.

eliantor commented 9 years ago

This should be fixed on master

dirkam commented 9 years ago

Awesome, thanks. Is there any planned release date for a new version of the Android SDK that will have this fix?

eliantor commented 9 years ago

Should be released today

dirkam commented 9 years ago

Sounds good, thanks for your help.

giastfader commented 9 years ago

Done: http://www.baasbox.com/baasbox-0-9-1-released/

dirkam commented 9 years ago

I've upgraded to 0.9.1, it seems that the issue has been fixed as I don't get this exception randomly, however, there are a certain situation where I still get the ProtocolException.

Steps to reproduce:

Login with a BaasBox User. Restart BaasBox server. Go to somewhere in the app where BaasBox server is called and it crashes with ProtocolException.

After crashing the app can be restarted and it works fine.

If Social Login is used then it doesn't crash as in this case the session token expires with restart and BaasInvalidSessionException is thrown, which can be handled correctly.

5.0.1 Lollipop.

java.lang.Error: Got a protocol exception while setting http method at com.baasbox.android.HttpUrlConnectionClient.setupConnectionForRequest(HttpUrlConnectionClient.java:244) at com.baasbox.android.HttpUrlConnectionClient.execute(HttpUrlConnectionClient.java:158) at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:151) at com.baasbox.android.NetworkTask.parseResponse(NetworkTask.java:74) at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:152) at com.baasbox.android.impl.Task.execute(Task.java:189) at com.baasbox.android.impl.Dispatcher$Worker.run(Dispatcher.java:170) Caused by: java.net.ProtocolException: content-length promised 17 bytes, but received 0 at com.android.okhttp.internal.http.RetryableSink.close(RetryableSink.java:50) at com.android.okio.RealBufferedSink.close(RealBufferedSink.java:174) at com.android.okio.RealBufferedSink$1.close(RealBufferedSink.java:142) at com.baasbox.android.HttpUrlConnectionClient.copyStream(HttpUrlConnectionClient.java:266) at com.baasbox.android.HttpUrlConnectionClient.addBody(HttpUrlConnectionClient.java:252) at com.baasbox.android.HttpUrlConnectionClient.setupConnectionForRequest(HttpUrlConnectionClient.java:228)             at com.baasbox.android.HttpUrlConnectionClient.execute(HttpUrlConnectionClient.java:158)             at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:151)             at com.baasbox.android.NetworkTask.parseResponse(NetworkTask.java:74)             at com.baasbox.android.NetworkTask.asyncCall(NetworkTask.java:152)             at com.baasbox.android.impl.Task.execute(Task.java:189)             at com.baasbox.android.impl.Dispatcher$Worker.run(Dispatcher.java:170)

dirkam commented 9 years ago

Did you happen to have a chance to take a look at this?