您的位置:首页 > 理论基础 > 计算机网络

OkHttp 的简单使用

2017-07-05 10:41 162 查看
OKHttp的依赖
compile'com.squareup.okhttp3:okhttp:3.2.0'
compile'com.squareup.okio:okio:1.7.0'
添加的权限
<uses-permissionandroid:name="android.permission.INTERNET"/>

GET异步请求

public void getAsynHttp() {
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
str = response.body().string();
Log.i("wangshu", str);

runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
HomeBean homeBean = gson.fromJson(str, HomeBean.class);
List<HomeBean.DataBean> data = homeBean.getData();
LvAdapter lvAdapter = new LvAdapter(MainActivity.this, data);
lv.setAdapter(lvAdapter);
Toast.makeText(getApplication(), "请求成功", Toast.LENGTH_SHORT).show();
}
});
}

});

}

异步POST请求
private void postAsynHttp() {
OkHttpClient mOkHttpClient = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("size", "10")
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
str1 = response.body().string();
Log.i("wangshu", str1);

runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson = new Gson();
HomeBean homeBean = gson.fromJson(str1, HomeBean.class);
List<HomeBean.DataBean> data = homeBean.getData();
LvAdapter lvAdapter = new LvAdapter(MainActivity.this, data);
lv.setAdapter(lvAdapter);
Toast.makeText(getApplicationContext(), "Post请求成功", Toast.LENGTH_SHORT).show();
}
});
}

});
}

4.异步下载文件
要记得加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


private void downAsynFile() {
OkHttpClient mOkHttpClient = new OkHttpClient();
String url = <
4000
strong>"http://news.op.wpscdn.cn/uploadfile/2017/0620/20170620101507878.jpeg"[/b];
Request request = new Request.Builder().url(url).build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) {
InputStream inputStream = response.body().byteStream();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(new File("/sdcard/wangshu.jpg"));
byte[] buffer = new byte[2048];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
fileOutputStream.flush();
} catch (IOException e) {
Log.i("wangshu", "IOException");
e.printStackTrace();
}

Log.d("wangshu", "文件下载成功");
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: