我正在尝试在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,标头等
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;
}
答案 0 :(得分:0)
如下更新您的请求方法代码:
@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进行替代。
答案 1 :(得分:0)
如下更新您的retrofit
请求,
retrofitResponse = new RetrofitResponse();
CommonPojo obj;
private void updateProfile() {
try {
showLoader();
retrofitResponse.setFirst_name(first_name.getText().toString().trim());
obj = new CommonPojo();
obj.setJSONobj(retrofitResponse);
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();
}
}
RetrofitResponse.java
public class RetrofitResponse {
String first_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
}
以Json
格式发送帖子数据,CommonPojo.java
public class CommonPojo {
private RetrofitResponse JSONobj;
public RetrofitResponse getJSONobj() {
return JSONobj;
}
public void setJSONobj(RetrofitResponse JSONobj) {
this.JSONobj = JSONobj;
}
}
尝试一下,让我知道是否需要帮助。
答案 2 :(得分:0)
感谢Viraj Patel,我已经以下面的方式实现了代码,并且可以正常工作:)
我的请求方法代码如下:
@Headers( "Content-Type: application/json; charset=utf-8")
@PUT("users/me/password-shares/")
fun sharePassword(@Body jsonObject: RequestBody): Call<ResponseBody>
调用方法为
val request = jsonObject.toString().toRequestBody("application/json".toMediaTypeOrNull());
var response = passwordService.sharePassword(request).execute()