标签:
首先介绍okhttp的简单使用:
于Android Studio可以选择添加依赖
compile ‘com.squareup.okhttp:okhttp:2.4.0‘
compile ‘com.squareup.okio:okio:1.5.0‘
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
.url("https://github.com/hongyangAndroid")
.build();
//new call
Call call = mOkHttpClient.newCall(request);
//请求加入调度
call.enqueue(new Callback()
{
@Override
public void onFailure(Request request, IOException e)
{
}
@Override
public void onResponse(final Response response) throws IOException
{
//String htmlStr = response.body().string();
}
});
response.body().string()
获得字符串
response.body().bytes()
获得二进制字节数组
response.body().byteStream()
获得输入流
Request request = buildMultipartFormRequest(
url, new File[]{file}, new String[]{fileKey}, null);
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add("username","张鸿洋");
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
mOkHttpClient.newCall(request).enqueue(new Callback(){});
标签:
原文地址:http://www.cnblogs.com/cfbaoshijie/p/5883038.html