目前retrofit速度最快,接下来是okhttp,最后是volley。
定义一个接口
public interface Service { @GET("/api/cook/list") Call<Tngou> getList(); }现在是模拟get请求,Get后面带的参数是网页的子目录,比如,www.baidu.com/api/cook/list,那get后面就带/api/cook/list
而baseurl则是主目录
Retrofit retrofit = new Retrofit.Builder() .baseUrl("www.baidu.com") .addConverterFactory(GsonConverterFactory.create()) .build(); Service service = retrofit.create(Service.class);//通过接口代理的发射得到
//得到那个接口
Call<Tngou> list = service.getList(); list.enqueue(this);
会重写到的方法
@Override public void onResponse(Call<Tngou> call, Response<Tngou> response) { List<Tngou_> tngou = response.body().getTngou(); mlistAdapter.addAll(tngou); Log.e("数据",response.body().getTngou().get(0).getName()); // Toast.makeText(MainActivity.this,"请求成功:"+response.body().getTngou().get(0).getName(),Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<Tngou> call, Throwable t) { // Toast.makeText(MainActivity.this,"请求失败:"+call.request().url(),Toast.LENGTH_SHORT).show(); }