2

我正在使用改造从 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 工作并在移动数据上失败。

4

4 回答 4

2

您可能没有 INTERNET 权限。将此添加到清单文件

<uses-permission android:name="android.permission.INTERNET" />
于 2019-07-16T11:24:27.593 回答
1

将网络配置文件添加到 res 文件夹

网络文件 in-res 文件夹

网络安全配置:

<network-security-config>
<domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">your domain here</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true">
    <trust-anchors>
        <certificates src="system" />
    </trust-anchors>
</base-config></network-security-config>

并在清单文件应用程序标记中添加一行

android:networkSecurityConfig="@xml/network_security_config"
于 2019-12-17T10:00:34.140 回答
0

由于它是服务器端处理的 HTTP 202 vs 200 问题,请尝试应用此问题: another answer on a different thread

于 2019-07-16T12:35:32.863 回答
0

使用您的改造代码配置OkHttp客户端。按照下面的步骤。

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .connectTimeout(20, TimeUnit.SECONDS)
    .writeTimeout(20, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();

然后在您构建 URL 的地方调用此代码。

public static Retrofit getClient() {
    if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

将连接超时添加到最大值。这可能是手机连接性低的问题。

于 2019-07-16T11:36:29.383 回答