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

时间:2017-01-30 06:27:24

标签: android post retrofit2

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

这是我的帖子回复enter image description here

我的代码是

 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接口,即使内部服务器错误相同。

请帮我解决这个问题。

提前致谢。

3 个答案:

答案 0 :(得分:4)

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

以下是它的演示:

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

例如:

@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类。

答案 1 :(得分:1)

在没有实际调试url的情况下判断有点困难。但是我遇到的常见原因可能如下。 1)必须正确区分基本网址和post方法中的网址。

2)如果这些参数是url的参数,那么尝试将它们作为例如

的主体发送
 public Call <String> getLoginCred(@Body HashMap<String,Object> userM);

如果参数是url的参数,那么只需使用@url进行url字符串调用。

检查BuildConfig.POSTSTATUS字符串是否正确,即您的基本URL应该是https:/192.168.1.1/,Poststatus字符串就像“/ api / User / AuthanticateUserById”所以/不会丢失和其他一些网址没有混合。

答案 2 :(得分:0)

use get method
 @FormUrlEncoded
    @GET(BuildConfig.POSTSTATUS)

url = t /api/mobile/user/pos url使用GET