Okio库是由square公司开发的,补充了java.io和java.nio的不足,更加方便,快速的访问、存储和处理你的数据。而OkHttp的底层也使用该库做为支持。能够让你的i/o操做更加的简单.java
Buffer类是方便访问和存储数据;有read和write方法git
Okio类是库提供的一个供外部使用的类,提供了大量的静态方法:github
其中有两个十分重要的接口Sink 和 Sourceapi
Sink:就至关于OutputStream 写操做 经过Sink能够得到一个BufferedSink服务器
Source:就至关于InputStream 写操做 经过Source能够得到一个BufferedSourcemarkdown
简单使用app
读取复制文件异步
try { //获取BufferedSource BufferedSource source = Okio.buffer(Okio.source(new FileInputStream("in.txt"))); //获取BufferedSink BufferedSink sink = Okio.buffer((Okio.sink(new FileOutputStream("out.txt")))); //将source所有读写到sink中 source.readAll(sink); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Okio工具类—ByteString类,这个类能够用来作各类变化,它将byte转会为String,而这个String能够是utf8的值,也能够是base64后的值,也能够是md5的值,也能够是sha256的值,总之就是各类变化,最后取得你想要的值。ide
String str = "this is new string"; ByteString string = ByteString.encodeUtf8(str); //打印string的base64值 Log.e(TAG, "onCreate: " + string.base64() ); //打印string的md5值 Log.e(TAG, "onCreate: " + string.md5().hex() );
E/MainActivity: onCreate: dGhpcyBpcyBuZXcgc3RyaW5n
E/MainActivity: onCreate: [hex=9c5e308d65c718bc69d616a0b530b004]工具
OKHttp
经常使用类:Request 同步提交请求和异步提交请求
Response:响应 包括响应头(Header) 响应码(code) 响应体(content)
从服务器同步获取数据
private void get() { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(new Runnable() { @Override public void run() { //获取构建器 Request.Builder builder = new Request.Builder(); //添加url地址 builder.url("https://raw.githubusercontent.com/square/okhttp/master/README.md"); //获取Request对象 Request request = builder.build(); Log.d(TAG, "run: " + request); //获取Call对象 Call call = mClient.newCall(request); try { //提交同步请求 Response response = call.execute(); if (response.isSuccessful()) { final String string = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { mContentTextView.setText(string); } }); } } catch (IOException e) { e.printStackTrace(); } } }); executor.shutdown(); }
从服务器异步获取数据
private void response() { Request.Builder builder = new Request.Builder(); builder.url("https://raw.githubusercontent.com/square/okhttp/master/README.md"); Request request = builder.build(); Call call = mClient.newCall(request); //提交异步请求 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d(TAG, "onFailure() called with: call = [" + call + "], e = [" + e + "]"); } @Override public void onResponse(Call call, Response response) throws IOException { Log.d(TAG, "onResponse() called with: call = [" + call + "], response = [" + response + "]"); //获取响应码 int code = response.code(); //获取响应头 以map形式存在,能够经过key值获取对应的value Headers headers = response.headers(); //获取响应体 String content = response.body().string(); final StringBuilder buf = new StringBuilder(); buf.append("code: " + code); buf.append("\nHeaders: \n" + headers); buf.append("\nbody: \n" + content); runOnUiThread(new Runnable() { @Override public void run() { mContentTextView.setText(buf.toString()); } }); } }); }
向服务器提交数据
private void post() { //从服务器开发者文档获取MediaType MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown; charset=utf-8"); String POST_URL = "https://api.github.com/markdown/raw"; Request.Builder builder = new Request.Builder(); builder.url(POST_URL); builder.post(RequestBody.create(MEDIA_TYPE_MARKDOWN, "Hello world github/linguist#1 **cool**, and #1!")); Request request = builder.build(); Call call = mClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { final String content = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { mContentTextView.setText(content); } }); } } }); }