gpt4 book ai didi

android - 使用 PUT 时的 RetroFit 2 Malforms URL

转载 作者:太空狗 更新时间:2023-10-29 14:50:27 24 4
gpt4 key购买 nike

我有一个改造 2 api。我的界面如下:

public interface Api {

...

@Headers({"Content-Type: application/json"})
@POST("/api/Login")
Call<User> login(@Body LoginRequest loginRequest);

@Headers({"Content-Type: application/json"})
@PUT("/api/User/{id}")
Call<User> updateDetails(@Path("id") String parentId, @Header("Cookie") String session, @Body UpdateProfileRequest profileDataRequest);
}

我按如下方式创建我的 Web 服务实例:

private static final Retrofit getAdapter() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient httpClient = new OkHttpClient();
httpClient.interceptors().add(logging);

String url = String.format("http://%s:%s", BuildConfig.SERVER, BuildConfig.PORT);
return new Retrofit.Builder()
.baseUrl(url)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}

现在,当我调用 LOGIN(GET)或 REGISTER(POST)时,它工作正常。但是,当我调用 PUT 方法时,它会使 URL 格式错误:

Response{protocol=http/1.1, code=404, message=Not Found, url=http://.../Account/Unauthorized/?ReturnUrl=%2Fapi%2FUser%2F9e5851bc-3f98-4e7c-82f7-b5ec805b53f6}

我尝试将其设为 POST,然后它不会使 URL 格式错误,但显然它不起作用,因为它需要是 PUT。我已经调试并且正确保存了 cookie 和 id 数据。

以防万一,这是我的库:​​

compile 'com.squareup.retrofit:retrofit:2.0.0-beta4'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
compile 'com.squareup.okhttp3:mockwebserver:3.0.1'

compile 'com.squareup.okio:okio:1.6.0'
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-moshi:2.0.0-beta2'
compile 'com.squareup:seismic:1.0.2'

编辑:
日志:

D/OkHttp: --> PUT /api/User/f475b862-xxxx-xxxx-a1e7-3309d5f2be11 HTTP/1.1 03-14 11:02:45.905 25738-26269/app.com D/OkHttp: Cookie: .AspNet.MyCookieMiddlewareInstance=cookiedata; path=/; httponly
03-14 11:02:45.905 25738-26269/app.com D/OkHttp: {"email":"user@email.com","id":"f475b862-xxxx-xxxx-a1e7-3309d5f2be11","name":"Joe Soap","password":"...."}
03-14 11:02:45.905 25738-26269/app.com D/OkHttp: --> END PUT (124-byte body)
03-14 11:02:45.930 25738-25796/app.com W/EGL_emulation: eglSurfaceAttrib not implemented
03-14 11:02:45.930 25738-25796/app.com W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0171a00, error=EGL_SUCCESS
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: <-- HTTP/1.1 404 Not Found (1842ms)
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: Server: nginx/1.4.6 (Ubuntu)
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: Date: Mon, 14 Mar 2016 08:58:02 GMT
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: Content-Type: text/html
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: Transfer-Encoding: chunked
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: Connection: keep-alive
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: OkHttp-Selected-Protocol: http/1.1
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: OkHttp-Sent-Millis: 1457946167561
03-14 11:02:47.748 25738-26269/app.com D/OkHttp: OkHttp-Received-Millis: 1457946167747
03-14 11:02:47.749 25738-26269/app.com D/OkHttp: <-- END HTTP (177-byte body)

从这里看起来它正在正确格式化 URL,但是当我从 RetroFit 获得响应对象时,它全是乱码。

最佳答案

所以事实证明内部服务器正在将错误 token 重定向到不存在的错误 token 页面。这导致了 404。RetroFit 工作正常。

我通过查看请求在服务器中采用的路径找到了这一点。

关于android - 使用 PUT 时的 RetroFit 2 Malforms URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35934461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com