成功或失败的方法不是在 android 中使用改造调用



In MainActivity.java//Root URL of our Web Service 公共静态最终字符串ROOT_URL = "http://pratikbutani.x10.mx";RestAdapter adapter = new RestAdapter.Builder(( .setEndpoint(ROOT_URL( .build((;

    //Creating an object of our api interface
    BooksAPI api = adapter.create(BooksAPI.class);
    //Defining the method
    api.getBooks(new Callback<List<Contact>>() {
        @Override
        public void success(List<Contact> list, Response response) {
            //Dismissing the loading progressbar
            //Storing the data in our list
            books = list;
            //Calling a method to show the list
            showList();
        }
        @Override
        public void failure(RetrofitError error) {
            //you can handle the errors here
        }
    });

公共接口 BooksAPI {

/*Retrofit get annotation with our URL
   And our method that will return us the list ob Book
*/
@GET("/json_data.json")
public void getBooks(Callback<List<Contact>> response);

}

//retrofit class

public class NetworkContext {
private static final String BASE_URL = "https://xyz/";
private Map<String, Object> services;
private Retrofit retrofit;
private static final NetworkContext INSTANCE = new NetworkContext();
private NetworkContext() {
    services = new HashMap<>();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();
}
public static NetworkContext getInstance() {
    return INSTANCE;
}
public synchronized <T> T getService(Class<T> clazz) {
    String key = clazz.getName();
    if (services.containsKey(key)) {
        return (T) services.get(key);
    } else {
        T newClass = retrofit.create(clazz);
        services.put(key, newClass);
        return newClass;
    }
}    
}

接口

public interface APIInterface {
@GET("will be attached with the base url")
Call<Model> getData();
}

//叫

   Call call = apiInterface.getData();
    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            Model model = (Model) response.body();
            // here you can retrieve data 
        }
        @Override
        public void onFailure(Call call, Throwable t) {
            call.cancel();
        }
    });

最新更新