Retrofit和OkHttp POST返回错误请求错误400

时间:2018-04-23 06:09:29

标签: android authentication post retrofit okhttp

在我的api包中

<?php
  if(!isset($_SESSION["redirect_to_success"]))
  {
     header("location:contactus.php");
  }
  else
  {
    unset($_SESSION["redirect_to_success"]);
  }
?>

}

public interface ApiCall {


    @Headers("Content-Type: application/json")
    @POST("message/save")
    Call<Repo> listRepos(@Header("authorization") String authorization , @Body EvreyId evreyId);
public class BaseParser<T> {

    @SerializedName("meta")
    private Meta mMeta;

    public Meta getMeta() {
        return mMeta;
    }

    public void setMeta(Meta meta) {
        mMeta = meta;
    }

    @SerializedName("data")
    private T mData;

    public T getData() {
        return mData;
    }

    public void setData(T data) {
        mData = data;
    }

    public boolean isSuccess(){
        if(mMeta==null ) return false;
        return mMeta.isSuccess();
    }
}
public class  EvreyId  {

    @SerializedName("DeviceId")
    @Expose
    private int DeviceId;

    @SerializedName("TripId")
    @Expose

    private int TripId;

    public EvreyId(int deviceId, int tripId) {
        DeviceId = deviceId;
        TripId = tripId;
    }

    public int getDeviceId() {
        return DeviceId;
    }

    public void setDeviceId(int deviceId) {
        DeviceId = deviceId;
    }

    public int getTripId() {
        return TripId;
    }

    public void setTripId(int tripId) {
        TripId = tripId;
    }

}

//在MainActivity中

public class Meta {

    @SerializedName("code")
    private int mCode;
    @SerializedName("error_message")
    private String mMessage;
    @SerializedName("error_type")
    private String mErrorType;

    public Boolean isSuccess() {
        return mCode / 100 == 2;
    }

    public int getCode() {
        return mCode;
    }

    public void setCode(int code) {
        mCode = code;
    }

    public String getMessage() {
        return mMessage;
    }

    public void setMessage(String message) {
        mMessage = message;
    }

    public String getErrorType() {
        return mErrorType;
    }

    public void setErrorType(String errorType) {
        mErrorType = errorType;
    }
}


public class Repo extends BaseParser {


}

在我的logcat中我得到400 Bad Request,我在OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .addHeader("Authorization","Bearer "+ Token) .build(); return chain.proceed(newRequest); } }).build(); Retrofit retrofit = new Retrofit.Builder() .client(client) .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); ApiCall service = retrofit.create(ApiCall.class); EvreyId evreyId=new EvreyId(deviceId,TripId); Call<Repo> repos = service.listRepos(authorization,evreyId); repos.enqueue(new Callback<Repo>() { @Override public void onResponse(Call<Repo> call, retrofit2.Response<Repo> response) { Repo repo=response.body(); try { try { Log.d(TAG, "response ErrorType :"+response.errorBody().string()); } catch (IOException e) { e.printStackTrace(); } } catch (NullPointerException e){ Log.e(TAG, "Null pointer error meta :"+e.getMessage()); e.printStackTrace(); } @Override public void onFailure(Call<Repo> call, Throwable t) { Log.d(TAG,"fail"+t.getMessage()); } }); class {中使用可选T 我的错在哪里???

0 个答案:

没有答案