Retrofit2 post方法显示服务器内部错误,但邮递员给出响应 [英] Retrofit2 post method showing server internal error but postman giving response
问题描述
我对回溯很陌生.我正在使用Retrofit2进行API集成.我有一个POST方法的API.我从邮递员那里寄出尸体,得到回应,但是当我以编程方式执行此操作时,我收到内部服务器错误".
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".
这是我的邮递员回复
我的代码是
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();
}
}
我的模型课是
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;
}
}
}
这是我的界面
@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);
即使相同的内部服务器错误,我也尝试了Multipart的界面.
I tried my interface with Multipart also, even though same internal server error.
请帮助我.
提前谢谢.
推荐答案
这是因为Post方法Retrofit 2中的一个小问题,称为content-type.您只需要将内容类型指定为标题的表单数据,然后访问它即可.另外,您需要创建一个POJO类,并使用该新注记的@Body通过该新创建的POJO类将其值传递给您的改造方法.
It's because of a small issue in post method Retrofit 2 called content-type. You just need to give the content type as form-data for header and then access it. Also, you need to make one POJO class and pass the values in your retrofit method via that newly made POJO class using the annotation as @Body.
这是它的演示:
在您的界面中声明此内容(此处将内容类型指定为表单数据,因为正在访问的后端正在接受内容类型作为表单数据)
In your interface, declare this(Here you give the content-type as form-data because the backend you are accessing, is accepting the content-type as form-data)
例如:
@Headers("Content-Type: form-data")
@POST(BuildConfig.POSTSTATUS)
Call<ApiModel> postData(@Header("Authorization") String Authorization, @Body WriteAPostModel writeAPostModel);
现在这是WriteAPostModel类,它是您的POJO类:
Now here is the WriteAPostModel class which is your POJO class:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Adhish on 02/02/17.
*/
public class WriteAPostModel {
@SerializedName("post_body")
@Expose
private String post_body;
@SerializedName("permission")
@Expose
private String permission;
@SerializedName("latitude")
@Expose
private String latitude;
@SerializedName("longitude")
@Expose
private String longitude;
@SerializedName("location")
@Expose
private String location;
public String getPost_image() {
return post_image;
}
public void setPost_image(String post_image) {
this.post_image = post_image;
}
@SerializedName("post_image")
@Expose
private String post_image;
public String getPost_body() {
return post_body;
}
public void setPost_body(String post_body) {
this.post_body = post_body;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getPermission() {
return permission;
}
public void setPermission(String permission) {
this.permission = permission;
}
}
最后,如下所述在您的活动中食用它们:
Finally, consume these in your activity like shown below:
private void savePost(String post_body,String post_image, 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);
WriteAPostModel writeAPostModel = new WriteAPostModel();
writeAPostModel.setPost_body(post_body);
writeAPostModel.setPermission(permission);
writeAPostModel.setLatitude(latitude);
writeAPostModel.setLongitude(longitude);
writeAPostModel.setLocation(location);
writeAPostModel.setPost_image(post_image);
Call<ApiModel> call = api.postData("JWT "+sharedPrefs.getPref(sharedPrefs.token), writeAPostModel);
/**
* Enqueue Callback will be call when get response...
*/
call.enqueue(new Callback<ApiModel>() {
@Override
public void onResponse(Call<ApiModel> call, Response<ApiModel> response) {
Log.d(" write post CODE", response.raw() + "");
if (response.isSuccessful()) {
Toast.makeText(ActivityWritePost.this,"Successfully Posted",Toast.LENGTH_SHORT).show();
finish();
} else {
}
}
@Override
public void onFailure(Call<ApiModel> call, Throwable t) {
//Dismiss Dialog
Log.d("POST_API", t.getCause() + "");
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
ApiModel是您的POJO类,用于获取响应.
Where ApiModel is your POJO class to get the responses.
这篇关于Retrofit2 post方法显示服务器内部错误,但邮递员给出响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!