OKHttp 的同步get
private final OkHttpClient client = new OkHttpClient(); public void execute() throw Exception{ Request request = new Request.Builder() .url("http://publicobject.com/helloworld.txt") .build(); Response response = client.newCall(request).execute(); if(response.isSuccessful()){ //do something } }
OKHttp 的异步get
private final OkHttpClient client = new OkHttpClient(); public void enqueue(){ Request request = new Request.Builder() .url() .builder(); client.newCall(request).enqueue(new Callback(){ @Override public void onFailure(){ //do something } @Override public void onResponse(Response response) throw IOException{ if(response.isSuccessful()){ //do something } } }); }