【问题标题】:Encoding of colon (:) with Google Cloud Endpoints, Android Studio使用 Google Cloud Endpoints、Android Studio 对冒号 (:) 进行编码
【发布时间】:2014-12-25 21:34:44
【问题描述】:

从生成的客户端库(使用默认的 Android Studio 1.02/Gradle 配置构建)向 AppEngine Dev Server(版本 1.9.15)传递字符串参数时,我发现字符串参数中的冒号不是 URL 编码的。然后,开发服务器将冒号解释为请求结构的一部分,返回 404。我还没有在生产环境中测试过。

我不知道这是否只是云端点库或开发服务器中的错误,但如果有人有修复或解决方法,我将不胜感激。手动 URLencoding 和解码参数可以工作,但不是很优雅。谢谢!

例子:

[Api].[method]("param:with:colon").execute().getItems();

产生请求:

GET /_ah/api/.../param:with:colon HTTP/1.1

以及回应:

HTTP/1.1 404 Not Found

不带冒号的字符串参数正常工作。

【问题讨论】:

    标签: android google-app-engine android-studio android-gradle-plugin google-cloud-endpoints


    【解决方案1】:

    这里的一种解决方法是通过在@ApiMethod 注释中提供显式路径属性来将此参数从路径参数转换为查询参数。

    在下面的示例代码中。默认情况下,参数“内容”是路径参数。 (这里的默认路径是“sayhi/{content}”)

     @ApiMethod(name = "sayHi", httpMethod = "get")
      public HelloGreeting sayHi(@Named("content") String content) throws Exception {
        return new HelloGreeting(content);
      }
    

    添加“路径”属性后,“内容”参数将通过/从查询参数传递和提取。

     @ApiMethod(name = "sayHi", httpMethod = "get", path="sayhi")
      public HelloGreeting sayHi(@Named("content") String content) throws Exception {
        return new HelloGreeting(content);
      }
    

    【讨论】:

    • 谢谢!这是一个很大的帮助。不过,默认情况下路径参数不干净似乎仍然很奇怪。
    猜你喜欢
    • 2014-10-03
    • 2013-07-20
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多