问题描述
我使用OKHTTP客户端联网在我的Android应用程序。
This示例说明了如何上传二进制文件。我想知道如何得到的二进制文件的InputStream的下载与OKHTTP客户端。
下面是示例的清单:
公共类InputStreamRequestBody扩展RequestBody {
私人的InputStream InputStream的;
私人的MediaType mediaType的;
公共静态RequestBody创建(最终的MediaType mediaType的,最终的InputStream的InputStream){
返回新InputStreamRequestBody(的InputStream,mediaType的);
}
私人InputStreamRequestBody(InputStream中的InputStream,的MediaType mediaType的){
this.inputStream =的InputStream;
this.mediaType = mediaType的;
}
@覆盖
公众的MediaType的contentType(){
返回mediaType的;
}
@覆盖
众长CONTENTLENGTH(){
尝试 {
返回inputStream.available();
}赶上(IOException异常E){
返回0;
}
}
@覆盖
公共无效的writeTo(BufferedSink汇)抛出IOException异常{
来源来源= NULL;
尝试 {
来源= Okio.source(InputStream的);
sink.writeAll(源);
} 最后 {
Util.closeQuietly(源);
}
}
}
目前的$ C $下简单的GET请求是:
OkHttpClient客户端=新OkHttpClient();
要求=新Request.Builder()。URL(URL字符串这里)
.addHeader(X-CSRFToken,csrftoken)
.addHeader(内容类型,应用/ JSON)建立()。
响应= getClient()新呼叫(要求).execute()。
现在我怎么转换响应的InputStream。同样的事情也从Apache的HTTP客户端这样的响应OKHTTP响应:
的InputStream是= response.getEntity()的getContent()。
编辑:
从下面的接受的答案。我修改code:
要求=新Request.Builder()URL(urlString).build()。
响应= getClient()新呼叫(要求).execute()。
InputStream的是= response.body()的字节流()。
的BufferedInputStream输入=新的BufferedInputStream(是);
的OutputStream输出=新的FileOutputStream(文件);
byte []的数据=新的字节[1024];
总长= 0;
而((计数= input.read(数据))!= - 1){
共有+ =计数;
output.write(数据,0,计数);
}
output.flush();
output.close();
input.close();
获取字节流从OKHTTP
我一直在周围挖OKHTTP的文档中,你需要走这条路
使用这个方法:
,所以你可以简单地使用一个BufferedReader或任何其他替代的
OkHTTPClient客户端=新OkHttpClient();
要求=新Request.Builder()。URL(URL字符串这里)
.addHeader(X-CSRFToken,csrftoken)
.addHeader(内容类型,应用/ JSON)建立()。
响应= getClient()新呼叫(要求).execute()。
在的InputStream = response.body()。字节流()
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(在));
字符串结果,线= reader.readLine();
结果=行;
而((行= reader.readLine())!= NULL){
结果+ =行;
}
的System.out.println(结果);
I am using OKHTTP client for networking in my android application.
This example shows how to upload binary file. I would like to know how to get inputstream of binary file downloading with OKHTTP client.
Here is the listing of the example :
public class InputStreamRequestBody extends RequestBody {
private InputStream inputStream;
private MediaType mediaType;
public static RequestBody create(final MediaType mediaType, final InputStream inputStream) {
return new InputStreamRequestBody(inputStream, mediaType);
}
private InputStreamRequestBody(InputStream inputStream, MediaType mediaType) {
this.inputStream = inputStream;
this.mediaType = mediaType;
}
@Override
public MediaType contentType() {
return mediaType;
}
@Override
public long contentLength() {
try {
return inputStream.available();
} catch (IOException e) {
return 0;
}
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(inputStream);
sink.writeAll(source);
} finally {
Util.closeQuietly(source);
}
}
}
Current code for simple get request is:
OkHttpClient client = new OkHttpClient();
request = new Request.Builder().url("URL string here")
.addHeader("X-CSRFToken", csrftoken)
.addHeader("Content-Type", "application/json").build();
response = getClient().newCall(request).execute();
Now how do I convert the response to Inputstream. Something similar to response from Apache HTTP Client like this for OKHTTP response:
InputStream is = response.getEntity().getContent();
EDIT:
Accepted answer from below.My modified code:
request = new Request.Builder().url(urlString).build();
response = getClient().newCall(request).execute();
InputStream is = response.body().byteStream();
BufferedInputStream input = new BufferedInputStream(is);
OutputStream output = new FileOutputStream(file);
byte[] data = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
Getting ByteStream from OKHTTP
I've been digging around in the Documentation of OKHTTP you need to go this way
use this method :
so you can simply use a BufferedReader or any other alternative
OkHTTPClient client = new OkHttpClient();
request = new Request.Builder().url("URL string here")
.addHeader("X-CSRFToken", csrftoken)
.addHeader("Content-Type", "application/json").build();
response = getClient().newCall(request).execute();
InputStream in = response.body().byteStream()
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String result, line = reader.readLine();
result = line;
while((line = reader.readLine()) != null) {
result += line;
}
System.out.println(result);
这篇关于下载从OKHTTP二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!