Android Retrofit获得空值

时间:2018-07-09 06:27:09

标签: android retrofit retrofit2

我收到onResponse作为成功消息,但response.tobody为空。

我尝试过的内容如下。

请帮助我。

主要活动

public class MainActivity extends AppCompatActivity {
BreakdownResponse breakdownResponse;
ApiInterface apiInterface;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button=(Button)findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signUp();
        }
    });
}



private void signUp() {
    // display a progress dialog
    final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setCancelable(false); // set cancelable to false
    progressDialog.setMessage("Please Wait"); // set message
    progressDialog.show(); // show progress dialog
    // Call<BreakdownResponse> call = apiInterface.registration(1,"1","1","101010");

    HashMap<String,response> map = new HashMap<>();
    map.put("breakdowncall",new response("1","1","1","101010"));
    Api.getClient().create(ApiInterface.class).registration(map).enqueue(new Callback<BreakdownResponse>() {
        @Override
        public void onResponse(Call<BreakdownResponse> call, Response<BreakdownResponse> response) {
            response.body();
            if (response.code() == 400) {
                Log.d("Error code 400",response.errorBody().toString());
            }
            if (response.code() == 400) {
                Gson gson = new GsonBuilder().create();
                BreakdownResponse mError=new BreakdownResponse();
                try {
                    mError= gson.fromJson(response.errorBody().string(),BreakdownResponse .class);
                    Log.e(" mError()","--" +  mError.toString());

                    Toast.makeText(getApplicationContext(), mError.toString(), Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    // handle failure to read error
                }
            }
            Toast.makeText(getApplicationContext(),"Success"+response.body(),Toast.LENGTH_LONG);
            Log.e("Success","--" + response.body());
        }

        @Override
        public void onFailure(Call<BreakdownResponse> call, Throwable t) {
            Toast.makeText(getApplicationContext(),"Failure",Toast.LENGTH_LONG);
            Log.e("Error","--" );
        }
    });


    /*Call<BreakdownResponse> call = apiInterface.registration(
            1,
            "",
            "",
            "101010"

    );



    call.enqueue(new Callback<BreakdownResponse>() {
        @Override
        public void onResponse(Call<BreakdownResponse> call, Response<BreakdownResponse> response) {
            response.body();
         Toast.makeText(getApplicationContext(),"Success"+response.body(),Toast.LENGTH_LONG);
         Log.e("Success","--" + response.body());
        }

        @Override
        public void onFailure(Call<BreakdownResponse> call, Throwable t) {
            Toast.makeText(getApplicationContext(),"Failure",Toast.LENGTH_LONG);
            Log.e("Error","--" );
        }
    });*/
    // Api is a class in which we define a method getClient() that returns the API Interface class object
    // registration is a POST request type method in which we are sending our field's data

}

}

Api接口

public interface ApiInterface {

@FormUrlEncoded // annotation used in POST type requests
@Headers({"authorization:Bearer")     
public Call<BreakdownResponse> registration(
        @FieldMap Map<String,response> Zone);

}

Api.java

public class Api {

private static Retrofit retrofit = null;

public static Retrofit getClient() {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new 
    OkHttpClient.Builder().addInterceptor(interceptor).build();


    retrofit = new Retrofit.Builder()
            .baseUrl("http://13.127.138.85/")
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();



    return retrofit;
}

}

BreakdownResponse.java

public class BreakdownResponse {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("customer")
@Expose
private String customer;
@SerializedName("zone")
@Expose
private String zone;
@SerializedName("region")
@Expose
private String region;
@SerializedName("rocode")
@Expose
private String rocode;
@SerializedName("roname")
@Expose
private Object roname;
@SerializedName("tankNo")
@Expose
private Object tankNo;
@SerializedName("product")
@Expose
private Object product;
@SerializedName("barriersConsole")
@Expose
private Object barriersConsole;
@SerializedName("dunumber")
@Expose
private Object dunumber;
@SerializedName("dumake")
@Expose
private Object dumake;
@SerializedName("serialNo")
@Expose
private Object serialNo;
@SerializedName("dumodel")
@Expose
private Object dumodel;
@SerializedName("thermalPrinterNumber")
@Expose
private Object thermalPrinterNumber;
@SerializedName("otherDevice")
@Expose
private Object otherDevice;
@SerializedName("customerTicketId")
@Expose
private Object customerTicketId;
@SerializedName("agsticketId")
@Expose
private Object agsticketId;
@SerializedName("rodealerNumber")
@Expose
private Integer rodealerNumber;
@SerializedName("rodealerName")
@Expose
private Object rodealerName;
@SerializedName("remarks")
@Expose
private Object remarks;


@SerializedName("engineerName")
@Expose
private Object engineerName;
@SerializedName("files")
@Expose
private List<Object> files = null;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getCustomer() {
    return customer;
}

public void setCustomer(String customer) {
    this.customer = customer;
}

public String getZone() {
    return zone;
}

public void setZone(String zone) {
    this.zone = zone;
}

public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}

public String getRocode() {
    return rocode;
}

public void setRocode(String rocode) {
    this.rocode = rocode;
}

public Object getRoname() {
    return roname;
}

public void setRoname(Object roname) {
    this.roname = roname;
}

public Object getTankNo() {
    return tankNo;
}

public void setTankNo(Object tankNo) {
    this.tankNo = tankNo;
}

public Object getProduct() {
    return product;
}

public void setProduct(Object product) {
    this.product = product;
}

public Object getBarriersConsole() {
    return barriersConsole;
}

public void setBarriersConsole(Object barriersConsole) {
    this.barriersConsole = barriersConsole;
}

public Object getDunumber() {
    return dunumber;
}

public void setDunumber(Object dunumber) {
    this.dunumber = dunumber;
}

public Object getDumake() {
    return dumake;
}

public void setDumake(Object dumake) {
    this.dumake = dumake;
}

public Object getSerialNo() {
    return serialNo;
}

public void setSerialNo(Object serialNo) {
    this.serialNo = serialNo;
}

public Object getDumodel() {
    return dumodel;
}

public void setDumodel(Object dumodel) {
    this.dumodel = dumodel;
}

public Object getThermalPrinterNumber() {
    return thermalPrinterNumber;
}

public void setThermalPrinterNumber(Object thermalPrinterNumber) {
    this.thermalPrinterNumber = thermalPrinterNumber;
}

public Object getOtherDevice() {
    return otherDevice;
}

public void setOtherDevice(Object otherDevice) {
    this.otherDevice = otherDevice;
}

public Object getCustomerTicketId() {
    return customerTicketId;
}

public void setCustomerTicketId(Object customerTicketId) {
    this.customerTicketId = customerTicketId;
}

public Object getAgsticketId() {
    return agsticketId;
}

public void setAgsticketId(Object agsticketId) {
    this.agsticketId = agsticketId;
}

public Integer getRodealerNumber() {
    return rodealerNumber;
}

public void setRodealerNumber(Integer rodealerNumber) {
    this.rodealerNumber = rodealerNumber;
}

public Object getRodealerName() {
    return rodealerName;
}

public void setRodealerName(Object rodealerName) {
    this.rodealerName = rodealerName;
}

public Object getRemarks() {
    return remarks;
}

public void setRemarks(Object remarks) {
    this.remarks = remarks;
}

public Object getEngineerName() {
    return engineerName;
}

public void setEngineerName(Object engineerName) {
    this.engineerName = engineerName;
}

public List<Object> getFiles() {
    return files;
}

public void setFiles(List<Object> files) {
    this.files = files;
}

@Override
public String toString() {
    return "BreakdownResponse{" +
            "id=" + id +
            ", customer='" + customer + '\'' +
            ", zone='" + zone + '\'' +
            ", region='" + region + '\'' +
            ", rocode='" + rocode + '\'' +
            ", roname=" + roname +
            ", tankNo=" + tankNo +
            ", product=" + product +
            ", barriersConsole=" + barriersConsole +
            ", dunumber=" + dunumber +
            ", dumake=" + dumake +
            ", serialNo=" + serialNo +
            ", dumodel=" + dumodel +
            ", thermalPrinterNumber=" + thermalPrinterNumber +
            ", otherDevice=" + otherDevice +
            ", customerTicketId=" + customerTicketId +
            ", agsticketId=" + agsticketId +
            ", rodealerNumber=" + rodealerNumber +
            ", rodealerName=" + rodealerName +
            ", remarks=" + remarks +
            ", engineerName=" + engineerName +
            ", files=" + files +
            '}';
}


}

Response.java

public class response {

@SerializedName("Customer")
@Expose
private String customer;
@SerializedName("Zone")
@Expose
private String zone;
@SerializedName("Region")
@Expose
private String region;
@SerializedName("Rocode")
@Expose
private String rocode;
@SerializedName("Roname")
@Expose
private String roname;
@SerializedName("TankNo")
@Expose
private String tankNo;
@SerializedName("Product")
@Expose
private String product;
@SerializedName("BarriersConsole")
@Expose
private String barriersConsole;
@SerializedName("Dunumber")
@Expose
private String dunumber;
@SerializedName("Dumake")
@Expose
private String dumake;
@SerializedName("SerialNo")
@Expose
private String serialNo;
@SerializedName("Dumodel")
@Expose
private String dumodel;
@SerializedName("TermalPrerNumber")
@Expose
private String termalPrerNumber;
@SerializedName("OtherDevice")
@Expose
private String otherDevice;
@SerializedName("CustomerTicketId")
@Expose
private String customerTicketId;
@SerializedName("AbcticketId")
@Expose
private String abcticketId;
@SerializedName("RodealerNumber")
@Expose
private String rodealerNumber;
@SerializedName("RodealerName")
@Expose
private String rodealerName;
@SerializedName("Remarks")
@Expose
private String remarks;
@SerializedName("EngineerName")
@Expose
private String engineerName;

public response(String customer, String zone, String region, String rocode) {
    this.customer = customer;
    this.zone = zone;
    this.region = region;
    this.rocode = rocode;
}

public String getCustomer() {
    return customer;
}

public void setCustomer(String customer) {
    this.customer = customer;
}

public String getZone() {
    return zone;
}

public void setZone(String zone) {
    this.zone = zone;
}

public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}

public String getRocode() {
    return rocode;
}

public void setRocode(String rocode) {
    this.rocode = rocode;
}

public String getRoname() {
    return roname;
}

public void setRoname(String roname) {
    this.roname = roname;
}

public String getTankNo() {
    return tankNo;
}

public void setTankNo(String tankNo) {
    this.tankNo = tankNo;
}

public String getProduct() {
    return product;
}

public void setProduct(String product) {
    this.product = product;
}

public String getBarriersConsole() {
    return barriersConsole;
}

public void setBarriersConsole(String barriersConsole) {
    this.barriersConsole = barriersConsole;
}

public String getDunumber() {
    return dunumber;
}

public void setDunumber(String dunumber) {
    this.dunumber = dunumber;
}

public String getDumake() {
    return dumake;
}

public void setDumake(String dumake) {
    this.dumake = dumake;
}

public String getSerialNo() {
    return serialNo;
}

public void setSerialNo(String serialNo) {
    this.serialNo = serialNo;
}

public String getDumodel() {
    return dumodel;
}

public void setDumodel(String dumodel) {
    this.dumodel = dumodel;
}

public String getTermalPrerNumber() {
    return termalPrerNumber;
}

public void setTermalPrerNumber(String termalPrerNumber) {
    this.termalPrerNumber = termalPrerNumber;
}

public String getOtherDevice() {
    return otherDevice;
}

public void setOtherDevice(String otherDevice) {
    this.otherDevice = otherDevice;
}

public String getCustomerTicketId() {
    return customerTicketId;
}

public void setCustomerTicketId(String customerTicketId) {
    this.customerTicketId = customerTicketId;
}

public String getAbcticketId() {
    return abcticketId;
}

public void setAbcticketId(String abcticketId) {
    this.abcticketId = abcticketId;
}

public String getRodealerNumber() {
    return rodealerNumber;
}

public void setRodealerNumber(String rodealerNumber) {
    this.rodealerNumber = rodealerNumber;
}

public String getRodealerName() {
    return rodealerName;
}

public void setRodealerName(String rodealerName) {
    this.rodealerName = rodealerName;
}

public String getRemarks() {
    return remarks;
}

public void setRemarks(String remarks) {
    this.remarks = remarks;
}

public String getEngineerName() {
    return engineerName;
}

public void setEngineerName(String engineerName) {
    this.engineerName = engineerName;
}

}

我的邮递员屏幕截图。

My postman screenshot

我的Logcat。

My Logcat

1 个答案:

答案 0 :(得分:0)

您是否尝试过传递参数:

 map.put("Customer", RequestBody.create(MediaType.parse("text/plain"), "1");
 map.put("Zone", RequestBody.create(MediaType.parse("text/plain"), "1");
 map.put("Region", RequestBody.create(MediaType.parse("text/plain"), "1");

并使用PartMap:

  @Multipart
    @POST("breakdowncall")
    Call<ModelTo> callMethod(
            @PartMap() Map<String, RequestBody> input
    );