I am very new to retroft. I am using retrofit2 for my API integration. I have an API for POST method. I am sending the body from postman and I am getting response, but when I am doing this programatically I am getting "Internal Server Error".
This is my post man response
And my code is
private void savePost(String post_body, String permission, String latitude, String longitude, String location) {
try {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService api = retrofit.create(ApiService.class);
/**
* Calling JSON
*/
Log.d("TOKEN","JWT "+sharedPrefs.getPref(sharedPrefs.token));
Call<PostModel> call = api.postData("JWT "+sharedPrefs.getPref(sharedPrefs.token), post_body, permission, latitude, longitude, location);
/* "JWT "+sharedPrefs.getPref(sharedPrefs.token)
*/
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<PostModel>() {
@Override
public void onResponse(Call<PostModel> call, Response<PostModel> response) {
Log.d(" write post CODE", response.raw() + "");
if (response.isSuccessful()) {
Log.e("post sucess..", "post sucess..");
Toast.makeText(ActivityWritePost.this,"Successfully Posted",Toast.LENGTH_SHORT).show();
} else {
}
}
@Override
public void onFailure(Call<PostModel> call, Throwable t) {
//Dismiss Dialog
Log.d("POST_API", t.getCause() + "");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
And my model class is
public class PostModel {
@SerializedName("status")
@Expose
private Integer status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private Data data;
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public class Data {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("user")
@Expose
private Integer user;
@SerializedName("post_image")
@Expose
private Object postImage;
@SerializedName("post_body")
@Expose
private String postBody;
@SerializedName("permission")
@Expose
private String permission;
@SerializedName("location")
@Expose
private String location;
@SerializedName("latitude")
@Expose
private Integer latitude;
@SerializedName("longitude")
@Expose
private Integer longitude;
@SerializedName("created")
@Expose
private String created;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUser() {
return user;
}
public void setUser(Integer user) {
this.user = user;
}
public Object getPostImage() {
return postImage;
}
public void setPostImage(Object postImage) {
this.postImage = postImage;
}
public String getPostBody() {
return postBody;
}
public void setPostBody(String postBody) {
this.postBody = postBody;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Integer getLatitude() {
return latitude;
}
public void setLatitude(Integer latitude) {
this.latitude = latitude;
}
public Integer getLongitude() {
return longitude;
}
public void setLongitude(Integer longitude) {
this.longitude = longitude;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
}
}
And this is my interface
@FormUrlEncoded
@POST(BuildConfig.POSTSTATUS)
Call<PostModel> postData(@Header("Authorization") String Authorization, @Field("post_body") String post_body, @Field("permission") String permission, @Field("latitude") String latitude, @Field("longitude") String longitude, @Field("location") String location);
I tried my interface with Multipart also, even though same internal server error.
Please help me out of this.
Thanks in advance.