使用Retrofit在Android中重新创建flask api调用

End*_*ave 8 android flask retrofit

我在服务器上有一个烧瓶app和api,它使用从终端发送的以下url

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

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

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);
    }
Run Code Online (Sandbox Code Playgroud)

和api课程

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

我只是在这一点上得到了通用错误,例如

 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
Run Code Online (Sandbox Code Playgroud)

这是我的第一个烧瓶应用程序,我不完全确定如何调试所以任何帮助在这里也是赞赏.

我也没有访问服务器日志


更新

为了尝试追踪问题,我编辑了服务器上的代码.如果我只是在api中返回一个字符串,那么改造就会收到响应.如果我尝试从改造中返回标头中发送的任何数据,我得到一个空响应,而curl请求将得到适当的响应.我已经尝试过response.data,以及response.json确保它不仅仅是一个json编码问题.但是,在执行此操作时,我需要删除"Content-type","application/json"标题,以便不能获得之前发布的400.

返回response.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 :(
Run Code Online (Sandbox Code Playgroud)

End*_*ave 2

好吧,看来需要增加赏金才能迫使我解决这个问题:(

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

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