【问题标题】:Sending a POST request to Google Forms returns code 400向 Google 表单发送 POST 请求返回代码 400
【发布时间】:2019-03-17 05:48:17
【问题描述】:

在我的 Android 应用中,我尝试向 Google 表单发送 POST 请求,但返回错误 400:

400 https://docs.google.com/forms/d/e/[form-id-number-here]/formResponse (114002ms)
content-type: text/html; charset=utf-8
x-robots-tag: noindex, nofollow, nosnippet
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: Mon, 01 Jan 1990 00:00:00 GMT
date: Sun, 17 Mar 2019 05:03:05 GMT
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
x-chromium-appcache-fallback-override: disallow-fallback
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
server: GSE
....

这是我的代码:

interface FormApi {
    @Headers("Content-Type: application/json",  "Accept: text/html", "Cache-Control: no-cache")
    @POST("{key}/formResponse")
    fun formResponseJson(@Path("key") key: String, @Body json: JsonObject): Call<ResponseBody>

}

用法:

val gson = GsonBuilder().setLenient().create()
val client = OkHttpClient.Builder()
            .readTimeout(timeout.toLong(), TimeUnit.SECONDS)
            .connectTimeout(timeout.toLong(), TimeUnit.SECONDS)
            .build()
val retrofit = Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(client)
            .build()
val api = retrofit.create(FormApi::class.java)
val json = dataObj.toJson()
val call = api.formResponseJson(key, json)
call.enqueue(CallBackResult(resultReceiver))   

请注意,json 对象的键值格式如下:{"entry.xxxxx1":"Test"}

有人知道为什么会返回此错误代码吗?有没有人成功向 Android 中的 Google 表单发送 POST?

谢谢。

【问题讨论】:

    标签: android rest google-api retrofit2 google-forms


    【解决方案1】:

    我进行了以下修改,代码现在可以发布到 Google 表单:

    在FormApi中:

        POST("{key}/formResponse")
        @FormUrlEncoded
        fun formResponse(@Path("key") key: String,  @FieldMap hashmap: HashMap<String, Any>): Call<ResponseBody>
    

    用法:

    val call = api.formResponse(key, hash)
    

    其中“hash”是具有字段实际值的 HashMap(不是 URLEncoded)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-11
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2015-01-25
      • 2020-11-03
      • 2023-03-11
      相关资源
      最近更新 更多