向Google Forms发送POST请求会返回代码400

问题描述 投票:0回答:1

在我的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”}

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

谢谢。

android rest google-api retrofit2 google-form
1个回答
0
投票

我做了以下修改,现在代码可以发布到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)

© www.soinside.com 2019 - 2024. All rights reserved.