Google Cloud Endpoint keeps throwing “unexpected e

2019-04-07 13:31发布

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.

2条回答
爷的心禁止访问
2楼-- · 2019-04-07 14:06

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-04-07 14:12

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)
查看更多
登录 后发表回答