我正在使用改造从 Web 服务中获取一些数据。改造通过 Wifi 给出了成功的响应,但是每当我使用移动数据时,改造都会给出失败响应。
Api 类
public static final String BASE_URL = "http://www.jivansath.com/api/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
RestApis 接口类
public interface RestApis {
@GET("User/LoadAlert")
Call<AlertsPOJO> getAlert();
}
MainActivity 类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RestApis apiService =
Api.getClient().create(RestApis.class);
Call<AlertsPOJO> call = apiService.getAlert();
call.enqueue(new Callback<AlertsPOJO>() {
@Override
public void onResponse(Call<AlertsPOJO>call, Response<AlertsPOJO> response) {
if (response.isSuccessful()){
AlertsPOJO.Response response1 = response.body().getResponse();
Toast.makeText(MainActivity.this, "Success " + response1.getId(), Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "Error " + response.errorBody(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<AlertsPOJO>call, Throwable t) {
// Log error here since request failed
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
}
}
警报POJO 类
public class AlertsPOJO {
@SerializedName("successcode")
@Expose
private Integer successcode;
@SerializedName("message")
@Expose
private String message;
@SerializedName("response")
@Expose
private Response response;
public Integer getSuccesscode() {
return successcode;
}
public void setSuccesscode(Integer successcode) {
this.successcode = successcode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
回复
public class Response {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("type")
@Expose
private Integer type;
@SerializedName("videourl")
@Expose
private String videourl;
@SerializedName("ImageURL")
@Expose
private String imageURL;
@SerializedName("Description")
@Expose
private String description;
@SerializedName("isactive")
@Expose
private Boolean isactive;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getVideourl() {
return videourl;
}
public void setVideourl(String videourl) {
this.videourl = videourl;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Boolean getIsactive() {
return isactive;
}
public void setIsactive(Boolean isactive) {
this.isactive = isactive;
}
}}
谁能指导我为什么 api 成功地通过 wifi 工作并在移动数据上失败。
