android–Retrofit2 post方法显示服务器内部错误但邮递员给出响应

android–Retrofit2 post方法显示服务器内部错误但邮递员给出响应,第1张

概述我很反击.我正在使用retrofit2进行API集成.我有一个POST方法的API.我从邮递员发送尸体,我得到了回复,但当我以编程方式执行此 *** 作时,我收到“内部服务器错误”.这是我的帖子man我的代码是privatevoidsavePost(Stringpost_body,Stringpermission,Stringlatitude,String

我很反击.我正在使用retrofit2进行API集成.我有一个POST方法的API.我从邮递员发送尸体,我得到了回复,但当我以编程方式执行此 *** 作时,我收到“内部服务器错误”.

这是我的帖子man

我的代码是

 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 pos@R_301_5998@;        @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 getPos@R_301_5998@() {            return pos@R_301_5998@;        }        public voID setPos@R_301_5998@(String pos@R_301_5998@) {            this.pos@R_301_5998@ = pos@R_301_5998@;        }        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接口.

请帮我解决这个问题.

提前致谢.

解决方法:

这是因为post方法Retrofit 2中的一个小问题叫做内容类型.您只需要将内容类型作为标题的表单数据,然后访问它.此外,您需要创建一个POJO类,并使用注释为@Body通过新创建的POJO类传递改造方法中的值.

以下是它的演示:

在你的界面中,声明这个(在这里你将content-type作为表单数据给出,因为你正在访问的后端,正在接受内容类型作为表单数据)

例如:

@headers("Content-Type: form-data")@POST(BuildConfig.postsTATUS)Call<APIModel> postData(@header("Authorization") String Authorization, @Body WriteAPostModel writeAPostModel);

现在这里是WriteAPostModel类,它是您的POJO类:

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;    }}

最后,在您的活动中使用这些,如下所示:

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类.

总结

以上是内存溢出为你收集整理的android – Retrofit2 post方法显示服务器内部错误但邮递员给出响应全部内容,希望文章能够帮你解决android – Retrofit2 post方法显示服务器内部错误但邮递员给出响应所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1108583.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存