Hi i am new to retrofit and facing problem to post json data for calling api's. I tried with many solutions but not getting the answer.
This is my code
Map<String,String> data=new HashMap<>();
data.put("email",email);
data.put("feedback",feedback);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), (new JSONObject(data)).toString());
Call<Feedback> call = apiInterface.feedbackResponse(accessToken,tokenType,client,expiry,uid,body);
call.enqueue(new Callback<Feedback>() {
@Override
public void onResponse(Call<Feedback> call, Response<Feedback> response) {
Log.e("Response",String.valueOf(response.code()));
Message message=response.body().getMessage();
String messageBody=message.toString();
String status=response.body().getStatus();
Toast.makeText(getApplicationContext(),status+" : "+messageBody,Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<Feedback> call, Throwable t) {
}
});
and My interface is
@POST("feedbacks")
public Call<Feedback> feedbackResponse(@Header(ApiConstant.ACCESS_TOKEN) String accessToken, @Header(ApiConstant.TOKEN_TYPE) String tokenType,
@Header(ApiConstant.CLIENT) String client, @Header(ApiConstant.EXPIRY) String expiry,
@Header(ApiConstant.UID) String uid, @Body RequestBody feedback);
Using headers like this is not giving problem as i am using it for every api. So dont consider headers is the problem.
My Response is
public class Feedback {
private Message message;
private String status;
private Data data;
public Message getMessage ()
{
return message;
}
public void setMessage (Message message)
{
this.message = message;
}
public String getStatus ()
{
return status;
}
public void setStatus (String status)
{
this.status = status;
}
public Data getData ()
{
return data;
}
public void setData (Data data)
{
this.data = data;
}
@Override
public String toString()
{
return "ClassPojo [message = "+message+", status = "+status+", data = "+data+"]";
}
}
Plese suggest me the right way.
Change signature to
@POST("feedbacks")
public Call<Feedback> feedbackResponse(@Header(ApiConstant.ACCESS_TOKEN) String accessToken, @Header(ApiConstant.TOKEN_TYPE) String tokenType,
@Header(ApiConstant.CLIENT) String client, @Header(ApiConstant.EXPIRY) String expiry,
@Header(ApiConstant.UID) String uid, @Body Map<String,Object> feedback);
and calling to
Map<String,Object> data=new HashMap<>();
data.put("email",email);
data.put("feedback",feedback);
apiInterface.feedbackResponse(accessToken,tokenType,client,expiry,uid,data);
or change @Body data type to JsonObject
and try
JsonObject data = new JsonObject();
data.addProperty("email",email);
data.add("feedback",new Gson().toJsonTree(feedback));
apiInterface.feedbackResponse(accessToken,tokenType,client,expiry,uid,data);
or you can also try @Body FeedbackRequestData data
. there are multiple way to do it.
apiInterface
must initialize with gson parser. And if header is required in every request then use okHttp interceptor.
Make sure your Feedback class is generated by http://www.jsonschema2pojo.org/ while choosing JSON and GSON
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.