gpt4 book ai didi

android - 如何使用 Retrofit 删除 JSON 请求中的 “nameValuePairs”?

转载 作者:行者123 更新时间:2023-11-30 05:04:23 26 4
gpt4 key购买 nike

我尝试在 POST 请求中发送原始数据,但 nameValuePairs 键与我的 JSON 连接在一起。

这是我的请求方法:-

@Headers( "Content-Type: application/json; charset=utf-8")
@POST("mpapi/seller/sellerprofilepost")
Call<ResponseBody>
updateProfile(@Header("Authorization") String token,
@Body JSONObject body);

我要发送这个:-

{
"firstname": "test1ff"
}

但在后端他们收到:-

{
"nameValuePairs":
{
"firstname":"test1ff"

}
}

调用api的方法:-

private void updateProfile() {
try {
showLoader();
JSONObject obj=new JSONObject();
obj.put("firstname",first_name.getText().toString().trim());
call = api.updateProfile("Bearer k8yu1q0k790lw5y4ta49alfbtsxoxs1w",obj);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
if (response.isSuccessful()) {
JSONObject obj = new JSONObject(response.body().string());
dialog = Func.OneButtonDialog(mContext, obj.getString("message"), ProfileScreen.this);
} else {
JSONObject obj = new JSONObject(response.errorBody().string());
dialog = Func.OneButtonDialog(mContext, obj.getString("message"), ProfileScreen.this);
}
} catch (Exception e) {
dialog = Func.OneButtonDialog(mContext, getResources().getString(R.string.ERROR_MSG), ProfileScreen.this);
e.printStackTrace();
}
hideLoader();
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
dialog = Func.OneButtonDialog(mContext, getResources().getString(R.string.ERROR_MSG), ProfileScreen.this);
hideLoader();
}
});
} catch (Exception e) {
dialog = Func.OneButtonDialog(mContext, getResources().getString(R.string.ERROR_MSG), this);
hideLoader();
e.printStackTrace();
}
}

改造调用方法:- 这是我设置基本 url、 header 等的改造调用方法

public Retrofit retrofitCall() {
String baseUrl = Constants.baseURL;
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(getSSLSocketFactory())
.retryOnConnectionFailure(true)
.addInterceptor(new AddHeaderInterceptor())
.readTimeout(40, TimeUnit.SECONDS)
.connectTimeout(40, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}

最佳答案

更新您的请求方法代码如下:

@Headers( "Content-Type: application/json; charset=utf-8")
@POST("mpapi/seller/sellerprofilepost")
Call<ResponseBody>
updateProfile(@Header("Authorization") String token,
@Body RequestBody body);

API调用方式:

    private void updateProfile() {
try {
showLoader();
JSONObject obj=new JSONObject();
obj.put("firstname",first_name.getText().toString().trim());
RequestBody bodyRequest = RequestBody.create(MediaType.parse("application/json"), obj.toString());
call = api.updateProfile("Bearer k8yu1q0k790lw5y4ta49alfbtsxoxs1w",bodyRequest);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
if (response.isSuccessful()) {
JSONObject obj = new JSONObject(response.body().string());
dialog = Func.OneButtonDialog(mContext, obj.getString("message"), ProfileScreen.this);
} else {
JSONObject obj = new JSONObject(response.errorBody().string());
dialog = Func.OneButtonDialog(mContext, obj.getString("message"), ProfileScreen.this);
}
} catch (Exception e) {
dialog = Func.OneButtonDialog(mContext, getResources().getString(R.string.ERROR_MSG), ProfileScreen.this);
e.printStackTrace();
}
hideLoader();
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
dialog = Func.OneButtonDialog(mContext, getResources().getString(R.string.ERROR_MSG), ProfileScreen.this);
hideLoader();
}
});
} catch (Exception e) {
dialog = Func.OneButtonDialog(mContext, getResources().getString(R.string.ERROR_MSG), this);
hideLoader();
e.printStackTrace();
}
}

或者您可以 refer this link用于替代方法。

关于android - 如何使用 Retrofit 删除 JSON 请求中的 “nameValuePairs”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54823769/

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