gpt4 book ai didi

java - 如何使用Retrofit2 POST数据?

转载 作者:行者123 更新时间:2023-12-02 09:52:08 25 4
gpt4 key购买 nike

我正在我的 Android 应用程序中尝试 Retrofit 2.4.0。我在哪里有登录 Activity 。我正在尝试 POST 请求,但它在 onResponse 方法中返回响应代码 404。但它与 POSTMAN 完美配合,我使用 HttpLoggingInterceptor 在日志中获取正确的数据。我尝试了很多方法来克服这个问题。我的服务代码是。

@POST("user_login")
@FormUrlEncoded
Call<ResponseBody> user_login(@Field("email") String email, @Field("password") String password);

@Multipart
@POST("user_login")
Call<ResponseBody> user_login(@PartMap() Map<String, RequestBody> bodyMap);

@Multipart
@POST("user_login")
Call<Status> user_login(@Part("email") RequestBody email, @Part("password") RequestBody password);

@POST("user_login")
Call<JSONObject> user_login(@Body String credentials);

@POST("user_login")
Call<User> user_login(@Body LoginCredentials credentials);

以上方法均无效。

我发布了一些我也在使用的其他方法。

@Provides
@Singleton
Cache provideOkHttpCache(TUK application) {
int cacheSize = 10 * 1024 * 1024; // 10 MB
return new Cache(application.getCacheDir(), cacheSize);
}

@Provides
@Singleton
Gson provideGson() {
GsonBuilder builder = new GsonBuilder();
// builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return builder.create();
}

@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cache(cache);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(message -> Logger.wtf("AppModule", message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(interceptor);
return builder.build();
}

@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient client) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(TUK.BASE_URL)
.client(client)
.build();
}

但我也在尝试其他 URL,使用 GET 方法也可以正常工作。

我的基本网址就像 "<a href="http://www.example.com/api/" rel="noreferrer noopener nofollow">http://www.example.com/api/</a>"和我的@PartMap()代码是

public static Map<String, RequestBody> getMap(LoginCredentials credentials) {
Map<String, RequestBody> map = new HashMap<>();
map.put("email", RequestBody.create(MultipartBody.FORM, credentials.getEmail()));
map.put("password", RequestBody.create(MultipartBody.FORM, credentials.getPassword()));
return map;
}

我的HttpLoggingInterceptor记录数据是

<-- 404 https://www.example.com/api/user_login (718ms)
content-type: application/json; charset=utf-8
.
.
.
.
{"status":false,"message":"Invalid User Email Or Password."}
<-- END HTTP (60-byte body)

还有我的onResponse记录数据的方法是

Response{protocol=h2, code=404, message=, url=https://www.example.com/api/user_login}

最后我的问题是如何使用 Retrofit 发布数据。

我现在就陷入了困境。我尝试过 stackoverflow 中的其他解决方案,但没有帮助。请大家帮我解决这个问题。任何帮助将不胜感激。谢谢。 This is the form data image. i am sending data in the BODY field

This is header data image

This is raw data image

最佳答案

您不需要使用多部分主体进行 POST 请求,可以通过以下方式轻松完成 下面的代码。例如,我有一个类似 http://example.com/api/ 的基本 URL

@FormUrlEncoded
@POST("login")
Call<LoginResponse> login(@Field("email") String email,
@Field("password") String password);

最终调用将被重定向至 http://example.com/api/login在上述情况下。

所以在这种情况下我的电话将被定向到 http://example.com/api/login 。或者其他问题可能出在您的 URL 中。由于您的基本网址是“http://www.example.com/api/ ”,并且通过加入 API 的 API 名称,它会变成“http://www.example.com/api//api/user_login ”,其中包含两个“//”,这可能会导致查找 API 时出错,因为它会抛出 404(未找到)。因此,请相应地调整您的基本网址。希望能有所帮助。

关于java - 如何使用Retrofit2 POST数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56250143/

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