I am trying to send raw data in POST request but nameValuePairs key get concated with my JSON.
Here is my request method:-
@Headers( "Content-Type: application/json; charset=utf-8")
@POST("mpapi/seller/sellerprofilepost")
Call<ResponseBody>
updateProfile(@Header("Authorization") String token,
@Body JSONObject body);
I am sending this:-
{
"firstname": "test1ff"
}
but at backend they are receiving :-
{
"nameValuePairs":
{
"firstname":"test1ff"
}
}
Method for calling 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();
}
}
Retrofit call method :- here is my retrofit call method where I am setting base url, headers etc
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;
}
Update your request method code as follow:
@Headers( "Content-Type: application/json; charset=utf-8")
@POST("mpapi/seller/sellerprofilepost")
Call<ResponseBody>
updateProfile(@Header("Authorization") String token,
@Body RequestBody body);
In API Calling Method:
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();
}
}
Or you can refer this link for alternate ways.