Google Cloud Endpoints error Caused by: java.lang.IllegalArgumentException: expected primitive class, but got

huangapple go评论133阅读模式
英文:

Google Cloud Endpoints error Caused by: java.lang.IllegalArgumentException: expected primitive class, but got

问题

以下是翻译好的内容:

我有一个简单的Google Cloud Endpoint,从我的Android应用程序调用它。它在相当长的一段时间内一直运行得很好。最近,我更新了Android Studio、Gradle插件和项目中的build.gradle文件。

自那以后,每次Android应用程序尝试调用该端点时,都会返回以下错误:

    Process: com.berylo.chat, PID: 26426
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        ...
        ...
     Caused by: java.lang.IllegalArgumentException: 
        at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:889)
        ...
        ...
     Caused by: java.lang.IllegalArgumentException: expected primitive class, but got: class com.crypto.berylo.com.clavePublica4Api.model.RespuestaAlCliente
        ...
        ...

这是端点的代码:

@ApiMethod(name = "obtenerDiasCaducidadClavesDeGrupo",
            path = "grupo",
            httpMethod = ApiMethod.HttpMethod.GET)
public RespuestaAlCliente obtenerDiasCaducidadClavesDeGrupo(User user)
            throws OAuthRequestException, IOException {
    String dias = "";
    if (user == null)
    {
        throw new OAuthRequestException("Usuario no valido");
    }
    else {
        dias = DIAS_HASTA_CADUCIDAD_DE_CLAVES_DE_GRUPO;
    }
    respuestaalcliente.setMessage(dias);
    logger.info("respuestaalcliente: " + respuestaalcliente);
    return respuestaalcliente;
}

这是Android应用程序的代码。触发错误的行用 THIS ------> 标记:

private void obtenernumeroDiasValidezClavesAsimetricas(final Context context){
    try {
        AsyncTask<Void, Void, String> miTareaAsincrona;
        miTareaAsincrona = new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... params) {
                try {
THIS ------&gt;        RespuestaAlCliente respuestaAlCliente = MisEndPointsClavePublica.serviceEndPoint.obtenerDiasCaducidadClaveAsimetrica().execute();
                    return respuestaAlCliente.getMessage();
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                if((result != null) && (!result.equals("") && (!result.equals("null")))) {
                    numeroDiasValidezClaveAsimetrica = parseInt(result);
                } else {
                    numeroDiasValidezClaveAsimetrica = 1;
                }
                try {
                    generateAsymmetricKeys(context, numeroDiasValidezClaveAsimetrica);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        };
        miTareaAsincrona.execute(null, null, null);
    } catch (Exception e){
        e.printStackTrace();
    }
}

RespuestaAlCliente是一个简单的类,用于包装端点的响应,因为原始数据不允许作为端点的响应:

public class RespuestaAlCliente {
	
	public String message;
	
	public RespuestaAlCliente() {}
	
	public RespuestaAlCliente(String message) {
	    this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
	    this.message = message;
	}
}

我尝试过恢复到以前的Android Studio版本、Gradle插件、库,甚至恢复了一些项目的备份。错误仍然存在。

我可能漏掉了什么,有什么想法吗?提前感谢。

编辑:我忘了提到,我在google-api-java-client的GitHub中找到了这个旧问题,有相同的堆栈跟踪。它可能与我的问题有关。https://github.com/googleapis/google-api-java-client/issues/1250

英文:

I have a simple Google Cloud Endpoint that is called from my Android app. It has been working like a charm for quite a long time. Recently, I have updated Android Studio, Gradle plugin, and the build.gradle files in the project.

Since then, the Android app is returning this error every time it tries to call to that endpoint:

    Process: com.berylo.chat, PID: 26426
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.IllegalArgumentException: 
        at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:889)
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:382)
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:355)
        at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)
        at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
        at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at com.berylo.chat.Encriptador$3.doInBackground(Encriptador.java:282)
        at com.berylo.chat.Encriptador$3.doInBackground(Encriptador.java:277)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)&#160;
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)&#160;
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)&#160;
        at java.lang.Thread.run(Thread.java:764)&#160;
     Caused by: java.lang.IllegalArgumentException: expected primitive class, but got: class com.crypto.berylo.com.clavePublica4Api.model.RespuestaAlCliente
        at com.google.api.client.util.Data.parsePrimitiveValue(Data.java:453)
        at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:859)
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:382)&#160;
        at com.google.api.client.json.JsonParser.parse(JsonParser.java:355)&#160;
        at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:87)&#160;
        at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)&#160;
        at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:459)&#160;
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)&#160;
        at com.berylo.chat.Encriptador$3.doInBackground(Encriptador.java:282)&#160;
        at com.berylo.chat.Encriptador$3.doInBackground(Encriptador.java:277)&#160;
        at android.os.AsyncTask$2.call(AsyncTask.java:333)&#160;
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)&#160;
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)&#160;
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)&#160;
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)&#160;
        at java.lang.Thread.run(Thread.java:764)

This is the Endpoint code:

@ApiMethod(name = &quot;obtenerDiasCaducidadClavesDeGrupo&quot;,
            path = &quot;grupo&quot;,
            httpMethod = ApiMethod.HttpMethod.GET)
    public RespuestaAlCliente obtenerDiasCaducidadClavesDeGrupo(User user)
            throws OAuthRequestException, IOException {
        String dias = &quot;&quot;;
        if (user == null)
        {
            throw new OAuthRequestException(&quot;Usuario no valido&quot;);
        }
        else {
            dias = DIAS_HASTA_CADUCIDAD_DE_CLAVES_DE_GRUPO;
        }
        respuestaalcliente.setMessage(dias);
        logger.info(&quot;respuestaalcliente: &quot; + respuestaalcliente);
        return respuestaalcliente;
    }

And this is the Android app code. The line which triggers de error is marked with THIS ------>

private void obtenernumeroDiasValidezClavesAsimetricas(final Context context){
        try {
            AsyncTask&lt;Void, Void, String&gt; miTareaAsincrona;
            miTareaAsincrona = new AsyncTask&lt;Void, Void, String&gt;() {

                @Override
                protected String doInBackground(Void... params) {
                    try {
THIS ------&gt;            RespuestaAlCliente respuestaAlCliente = MisEndPointsClavePublica.serviceEndPoint.obtenerDiasCaducidadClaveAsimetrica().execute();
                        return respuestaAlCliente.getMessage();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                    }
                }

                @Override
                protected void onPostExecute(String result) {
                    if((result != null) &amp;&amp; (!result.equals(&quot;&quot;) &amp;&amp; (!result.equals(&quot;null&quot;)))) {
                        numeroDiasValidezClaveAsimetrica = parseInt(result);
                    } else {
                        numeroDiasValidezClaveAsimetrica = 1;
                    }
                    try {
                        generateAsymmetricKeys(context, numeroDiasValidezClaveAsimetrica);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            };
            miTareaAsincrona.execute(null, null, null);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

RespuestaAlCliente is a simple class that wraps the endpoint's response, as primitives are not allowed as endpoints responses

public class RespuestaAlCliente {
	
	public String message;
	
	public RespuestaAlCliente() {}
	
	public RespuestaAlCliente(String message) {
	    this.message = message;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
	    this.message = message;
	}
}

I have tried reverting to the previous Android Studio version, gradle plugin, libraries, and even restoring some project's backups. The error persists.

I am missing something, any ideas? Thanks in advance.

Edit: I forgot to mention that I found this old issue in google-api-java-client's github, with the same stacktrace. It could be related to my problem. https://github.com/googleapis/google-api-java-client/issues/1250

答案1

得分: 0

抱歉,这是我的错 Google Cloud Endpoints error Caused by: java.lang.IllegalArgumentException: expected primitive class, but got

我希望一些人在我发布网站之前能看到它,所以我在我的 web.xml 文件中上传了一些安全约束,并为那些用户授予权限。这些限制阻止了 Android 应用程序访问后端端点...

我希望没有人在这上面浪费时间。对于可能造成的任何不便,我感到很抱歉。

英文:

Well, shame on me, this was my fault Google Cloud Endpoints error Caused by: java.lang.IllegalArgumentException: expected primitive class, but got

I wanted some people to see my website before publishing it, so I uploaded it with some security constraints in my web.xml file, and gave permissions to those users. Those restrictions prevented the Android app to reach the backend endpoints...

I hope nobody wasted time on this. I am sorry for any inconvenience it may have caused.

huangapple
  • 本文由 发表于 2020年3月16日 19:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定