7

Does anyone know why Google Cloud Endpoint keeps throwing unexpected end of stream exception even before my app engine instance is actually reached? I keep getting the following error when I call my endpoint. In most places the error shows after every other call; in rare others it's consistent.

05-06 18:32:28.335: W/System.err(11783): java.io.IOException: unexpected end of stream
05-06 18:32:28.343: W/System.err(11783):    at libcore.net.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:58)
05-06 18:32:28.343: W/System.err(11783):    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:82)
05-06 18:32:28.343: W/System.err(11783):    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:980)
05-06 18:32:28.343: W/System.err(11783):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
05-06 18:32:28.343: W/System.err(11783):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
05-06 18:32:28.343: W/System.err(11783):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
…

05-06 18:32:28.343: W/System.err(11783):    at android.os.AsyncTask.finish(AsyncTask.java:631)
05-06 18:32:28.343: W/System.err(11783):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-06 18:32:28.343: W/System.err(11783):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
05-06 18:32:28.343: W/System.err(11783):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 18:32:28.343: W/System.err(11783):    at android.os.Looper.loop(Looper.java:137)
05-06 18:32:28.343: W/System.err(11783):    at android.app.ActivityThread.main(ActivityThread.java:4849)
05-06 18:32:28.343: W/System.err(11783):    at java.lang.reflect.Method.invokeNative(Native Method)
05-06 18:32:28.343: W/System.err(11783):    at java.lang.reflect.Method.invoke(Method.java:511)
05-06 18:32:28.343: W/System.err(11783):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-06 18:32:28.343: W/System.err(11783):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-06 18:32:28.343: W/System.err(11783):    at dalvik.system.NativeStart.main(Native Method)

BTW: I get this error even for small operations such as verifying a token, which could be a string of 20 to 50 chars.

Pouton Gerald
  • 1,625
  • 22
  • 32
  • Could you provide your endpoint code please? – MikO May 07 '13 at 09:14
  • There is an open GAE issue for this: https://code.google.com/p/googleappengine/issues/detail?id=9291 – Tom Oct 16 '13 at 18:10
  • And an open google java api client issue: https://code.google.com/p/google-http-java-client/issues/detail?id=230 – Tom Oct 16 '13 at 18:11

2 Answers2

1

I am getting the same issue too, every other time I get this "unexpected end of stream" IOE exception. As you say no logs are recorded in appengine. I have a class with several endpoints but this only happens to one of them.

This is the structure of the Api Method:

@ApiMethod(name = "blablabla", httpMethod = HttpMethod.POST)
public static ooooo createCDR(@Named("iiii") String iiii,
        @Named("uuuu") String uuuu, @Named("cccc") Long cccc,
        @Named("aaaa") int aaaa, User user)
Miguel
  • 73
  • 5
  • It looks it is not related with the particular Api method being called. I have tried others that were working fine in other classes but in this particular one they fail. The only difference is that all endpoint that work ok are called from activities and this class that makes the method calls fail is a handler called by a service. – Miguel May 10 '13 at 01:48
  • 1
    I have created an appengine issue: https://code.google.com/p/googleappengine/issues/detail?id=9291&thanks=9291&ts=1368199444 – Miguel May 10 '13 at 16:43
  • can you add extra logging in app engine application to capture errors and see if you can get any clue . Check this google document with sample to get an idea : [logging in appengine](https://developers.google.com/appengine/articles/logging) – tony m May 18 '13 at 12:39
  • Tony, as Pouton mentions in his question the appengine instance is not even reached. It seems to be an issue in the Android Endpoints implementation. – Miguel May 18 '13 at 21:25
  • @Miguel Have you found a workaround for this? I get this error occasionally when endpoint backend calls are made from an android service. – plspl Jun 10 '13 at 17:30
  • @plsp No so far I haven't... an it is quite frustrating. – Miguel Jun 13 '13 at 11:02
  • @KillerFrca I am calling endpoints inside of a Thread that runs asynchronously (I prefer this option better than using AsyncTask). This can't be the reason. – Miguel Jun 13 '13 at 11:04
  • I set the connection close property and so far I have not hit this error. But then again, I don't have a wide range of devices to test with. Let me know if this works for you. public Send send(com.google.api.services.devices.model.ServiceMessagesContentMessage content) throws java.io.IOException { Send result = new Send(content); result.getRequestHeaders().set("connection", "close"); initialize(result); return result; } – plspl Jun 15 '13 at 00:57
  • Thanks plspl. I check your approach in Android 4.2. It works fine. – KNaito Nov 18 '14 at 16:11
  • Sorry, still the error sometimes occurs. I try to other approach. http://stackoverflow.com/questions/17962819/unexpected-end-of-stream-thrown-during-nethttprequest-execute-when-uploading – KNaito Nov 19 '14 at 03:02
1

I had same issue. Problem is, that communication with endpoints must NOT be running on ui/main thread. Easiest way is to create simple ASyncTask for example like thislike this:

private class InsertTask extends AsyncTask<Void, Void, Void> {
    Exception exceptionThrown = null;
    TargetEndpoint targetEndpoint;
    Target target;

    public InsertMessageTask(Activity activity, TargetEndpoint targetEndpoint, Target target) {
        this.messageEndpoint= messageEndpoint;
        this.target= target;
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            targetEndpoint.insertTarget(target).execute();
        } catch (IOException e) {
            exceptionThrown = e;
        }

        return null;
    }

    protected void onPostExecute(Void arg) {
        TheActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (exceptionThrown == null)
                    Toast.makeText(TheActivity.this, "Success!", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(TheActivity.this, "Error occured: '" + exceptionThrown.getMessage(), Toast.LENGTH_LONG).show();

            }
        });
    }
}

I agree that error message could be more specified, but it has its reason. You dont want to run time expensive methods on ui thread, as it may reduce its performance.

KillerFrca
  • 135
  • 1
  • 1
  • 6