I. Interface
Public Interface HttpListener { void onfinish (String reponse); void OnError (Exception e);}
Two. Okhttputil
Public classOkhttputil { Public Static voidasyncget (String URL, final HttpListener listener) {okhttpclient http=Newokhttpclient (); Final Request Request=Newrequest.builder (). URL (URL). build (); Call Call=Http.newcall (Request); Call.enqueue (NewCallback () {@Override Public voidonfailure (call call, IOException e) {listener.onerror (e); } @Override Public voidOnresponse (call call, Response Response) throws IOException {Listener.onfinish (Response.body ().string()); } }); }}
Three. Call
Okhttputil.asyncget ("http://www.baidu.com"new HttpListener () { @ Override publicvoid onfinish (String reponse) { log.e (" Log", reponse); } @Override publicvoid onError (Exception e) { e.printstacktrace (); } } );
Okhttpclient Simple Package