Retrofit2 post method showing server internal error but postman giving response

与世无争的帅哥 提交于 2019-12-06 02:11:27

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.

Here is the demonstration of it:

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)

Ex:

@Headers("Content-Type: form-data")
@POST(BuildConfig.POSTSTATUS)
Call<ApiModel> postData(@Header("Authorization") String Authorization, @Body WriteAPostModel writeAPostModel);

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

Where ApiModel is your POJO class to get the responses.

It is bit difficult to tell without actually debugging the url.But usual reasons could be as follows, which I encountered. 1) The base url and the url in post method have to be differentiated properly.

2) If those parameters are parameters for url, then try to send them as a body for eg

 public Call <String> getLoginCred(@Body HashMap<String,Object> userM);

if the parameters are parameters to the url then simply make an url string call with @url.

Check if the BuildConfig.POSTSTATUS string is proper, ie your base url should be something like https:/192.168.1.1/ and Poststatus string is like "/api/User/AuthanticateUserById" so the / is not missing and some other url is not mixed with it.

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

url=t/api/mobile/user/pos url uses GET

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!