gpt4 book ai didi

android - 如何使用 POST 方法在 android 中使用 Retrofit 发送原始 JSON

转载 作者:行者123 更新时间:2023-11-29 15:00:04 28 4
gpt4 key购买 nike

我想发布以下 JSON,

{
"name": {
"firstName": "f_name",
"lastName": "l_name"
},
"password": "mypassword123",
"email": "test.mail@gmail.com"
}

接口(interface)内部的注册方法是,

@POST("user/createuser")
Call<RegisterResponseModel> register(@Body RegisterModel body);

使用像这样的注册方法,

Name name = new Name("f_name", "l_name");

RegisterModel registerModel = new RegisterModel(name, "mypassword123", "test.mail@gmail.com");

Call<RegisterResponseModel> res = apiService.register(registerModel);

但无法达到我想要的,请帮助我实现我需要的。提前致谢

最佳答案

试试这个

我在尝试仅以 原始 POST 数据 形式时遇到了同样的问题

  1. 您的 API 接口(interface)

@POST("your_url_here")
Call<Object> getUser(@Body Map<String, String> body);

  1. 调用您的

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);

try {
Map<String, String> requestBody = new HashMap<>();
requestBody.put("email", "abc@gmail.com");
requestBody.put("password", "123678");
Call<Object> call=apiInterface.getUser(requestBody);
call.enqueue(new Callback<Object>() {
@Override
public void onResponse(Call<Object> call, Response<Object> response) {
try {
JSONObject object=new JSONObject(new Gson().toJson(response.body()));
Log.e("TAG", "onResponse: "+object );
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<Object> call, Throwable t) {
}
});
} catch (Exception e) {
e.printStackTrace();
}

关于android - 如何使用 POST 方法在 android 中使用 Retrofit 发送原始 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48100284/

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