Android改造Web服务无法正常工作

时间:2018-06-23 13:24:32

标签: android retrofit

我有一个在线数据库,我想使用改造将数据发送到我的android应用程序,这里有我的完整代码,我不知道为什么它不起作用 here is my data link

Urls.java

public class Urls {
    public static final String MAIN_URL = "https://codemac.000webhostapp.com/";
}

API.java

public interface API {

    @POST("MainCat.php")
    Call<List<CatResp>> getCat();

}

CatResp.java

public class CatResp {

    @SerializedName("title")
    public String title;

    @SerializedName("img")
    public String img;

    @SerializedName("type")
    public String type;

    @SerializedName("view")
    public String view;

}

WebService.java

public class WebService {
    private static WebService instance;
    private API api;

    public WebService() {

        OkHttpClient client = new OkHttpClient.Builder().build();
        Retrofit retrofit = new Retrofit.Builder().client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Urls.MAIN_URL)
                .build();

        api = retrofit.create(API.class);
    }

    public static WebService getInstance() {
        if (instance == null) {
            instance = new WebService();
        }
        return instance;
    }

    public API getApi() {
        return api;
    }
}

MainActivity.java

WebService.getInstance().getApi().getCat().enqueue(new Callback<List<CatResp>>() {
            @Override
            public void onResponse(Call<List<CatResp>> call, Response<List<CatResp>> response) {
                Toast.makeText(MainActivity.this,"Success",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<List<CatResp>> call, Throwable t) {
                Toast.makeText(MainActivity.this,"Fail",Toast.LENGTH_LONG).show();
            }
        });

问题出在哪里?调用onFailure并烤掉“ Fail”

0 个答案:

没有答案