起因:
OkHttpUtils类使用遇到的一个坑记录,在使用流文件的时候,不能进行两次的response.body().byteStream(),否则第二次调用该方法的时候流是关闭的,不能进行使用了;
/**
* post请求,返回InputStream
*
* @param url
* @param json
* @return
* @throws IOException
*/
public InputStream postInputStream(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = mOkHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().byteStream();
} else {
throw new IOException("Unexpected code " + response);
}
}
/**
* Returns a new {@code BufferedSource} that can read data from this {@code BufferedSource}
* without consuming it. The returned source becomes invalid once this source is next read or
* closed.
*
* For example, we can use {@code peek()} to lookahead and read the same data multiple times.
*
* <pre> {@code
*
* Buffer buffer = new Buffer();
* buffer.writeUtf8("abcdefghi");
*
* buffer.readUtf8(3) // returns "abc", buffer contains "defghi"
*
* BufferedSource peek = buffer.peek();
* peek.readUtf8(3); // returns "def", buffer contains "defghi"
* peek.readUtf8(3); // returns "ghi", buffer contains "defghi"
*
* buffer.readUtf8(3); // returns "def", buffer contains "ghi"
* }</pre>
*/
BufferedSource peek();
/** Returns an input stream that reads from this source. */
InputStream inputStream();
关于流文件读取一次之后再次时候流会关闭的最底层方法;一个坑;