使用 Retrofit 在 Android 中重新创建烧瓶 api

Recreating flask api call in Android with Retrofit

我有一个 Flask 应用程序,并且 api 在服务器上可以使用从终端发送的以下 url

curl -i -H "Content-type: application/json" -X GET http://myapp.com/890/14000/10000/007 -d '{"id":"3240f056c8f5fb"}'

我正尝试在 Android 上使用改造来重新创建它。我使用的是 1.7 版,因为它适用于此处未显示的一些遗留代码。这是应用程序的相关部分 class

DavesApi buildDavesApi() {
        Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .create();

        return new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL) // change this to FULL to see full JSON response
                .setEndpoint(DAVESENDPOINT)
                .setConverter(new GsonConverter(gson))
                .setRequestInterceptor(new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade request) {
                        request.addHeader("Content-type", "application/json");
                    }
                })
                .build()
                .create(DavesApi.class);
    }

和 api class

public interface DavesApi {
    @Headers("\{\"id\"\:\"3240f056c8f5fb\"\}")
    @GET("/{importantnumber}/14000/10000/007")
    void getThumbnail(@Path("importantnumber") Integer id, Callback<DavesActivity> activity);
}

此时我只收到一般错误,例如

 D/Retrofit: ---> HTTP GET http://myurl.com/420059/14000/10000/007
 D/Retrofit: '{"id": "3240f056c8f5fb"}'
 D/Retrofit: Content-Type: application/json
 D/Retrofit: ---> END HTTP (no body)
 D/Retrofit: <--- HTTP 400 http://myurl.com/420059/14000/10000/007 (2653ms)
 D/Retrofit: : HTTP/1.1 400 BAD REQUEST
 D/Retrofit: Connection: keep-alive
 D/Retrofit: Content-Length: 95
 D/Retrofit: Content-Type: application/json
 D/Retrofit: Date: Sun, 29 Nov 2015 23:08:45 GMT
 D/Retrofit: Server: ngx_openresty/1.4.3.6
 D/Retrofit: X-Android-Received-Millis: 1448838524561
 D/Retrofit: X-Android-Response-Source: NETWORK 400
 D/Retrofit: X-Android-Sent-Millis: 1448838523377
 D/Retrofit: {"description": "The browser (or proxy) sent a request that this server could not understand."}
 D/Retrofit: <--- END HTTP (95-byte body)
 E/activity_fragment: retrofit.RetrofitError: 400 BAD REQUEST

这是我的第一个 flask 应用程序,我不完全确定如何调试,因此也感谢您提供的任何帮助。

我也没有访问服务器日志的权限


更新

为了尝试找出问题所在,我在服务器上编辑了代码。如果我只是 return 中的一个字符串 api 然后改装会收到响应。如果我尝试 return 在 header 中发送的任何数据,我得到一个空响应,而 curl 请求将得到适当的响应。我已经尝试使用 response.dataresponse.json 来确保它不仅仅是一个 json 编码问题。但是,在执行此操作时,我需要删除 "Content-type"、"application/json" header 以避免之前发布的 400。

returnresponse.data

时的新日志
 D/Retrofit: ---> HTTP GET http://myurl.com/420059/14000/10000/007
 D/Retrofit: test: header
 D/Retrofit: ---> END HTTP (no body)
 D/Retrofit: <--- HTTP 200 http://myurl.com/420059/14000/10000/007 (316ms)
 D/Retrofit: : HTTP/1.1 200 OK
 D/Retrofit: Connection: keep-alive
 D/Retrofit: Content-Length: 0
 D/Retrofit: Content-Type: text/html; charset=utf-8
 D/Retrofit: Date: Mon, 30 Nov 2015 20:34:54 GMT
 D/Retrofit: Server: ngx_openresty/1.4.3.6
 D/Retrofit: X-Android-Received-Millis: 1448915692589
 D/Retrofit: X-Android-Response-Source: NETWORK 200
 D/Retrofit: X-Android-Sent-Millis: 1448915692449
 D/Retrofit: X-Clacks-Overhead: GNU Terry Pratchett
 D/Retrofit: <--- END HTTP (0-byte body) // empty body :(

好吧,似乎需要额外的赏金才能迫使我解决这个问题:(

这只是我对基本卷曲的误解。显然,curl 请求将数据放入 body,而我将其用于另一个 header。只需更改服务器上的代码以查找 request.header 或将 id 更改为改造请求的 @data 部分即可。

不敢相信我在这个盲目明显的错误上浪费了这么多时间和赏金。