android okhttp的简单使用(get,post)
2017-03-27 16:00
501 查看
添加依赖
compile 'com.squareup.okhttp3:okhttp:3.6.0'
Get请求
private void initGetOkHttp(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.get()
.url("https:www.baidu.com")
.build();
Call call = client.newCall(request);
//异步调用,并设置回调函数
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(OkHttpActivity.this, "get failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String res = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("--->","====> get" + res);
}
});
}
});
}
Post请求
private void initPostOkHttp(){
OkHttpClient client = new OkHttpClient();
FormBody formBody = new FormBody.Builder()
.add("username", "admin")
.add("password", "admin")
.build();
final Request request = new Request.Builder()
.url("http://www.jianshu.com/")
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(OkHttpActivity.this, "Post Failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String res = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("--->","====> post" + res);
}
});
}
});
}
compile 'com.squareup.okhttp3:okhttp:3.6.0'
Get请求
private void initGetOkHttp(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.get()
.url("https:www.baidu.com")
.build();
Call call = client.newCall(request);
//异步调用,并设置回调函数
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(OkHttpActivity.this, "get failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String res = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("--->","====> get" + res);
}
});
}
});
}
Post请求
private void initPostOkHttp(){
OkHttpClient client = new OkHttpClient();
FormBody formBody = new FormBody.Builder()
.add("username", "admin")
.add("password", "admin")
.build();
final Request request = new Request.Builder()
.url("http://www.jianshu.com/")
.post(formBody)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Toast.makeText(OkHttpActivity.this, "Post Failed", Toast.LENGTH_SHORT).show();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String res = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e("--->","====> post" + res);
}
});
}
});
}
相关文章推荐
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- Android中Okhttp进行get和post提交及数据请求的基本使用
- 【Android】Retrofit的使用(4)-Retrofit进行简单的GET和POST访问操作
- "Android下使用HttpUrlConntion"进行最简单的get,post请求(包括创建服务器)
- Android进阶之路 - okHttp中Get与Post初级使用(一)
- Android中使用OkHttp包处理HTTP的get和post请求的方法
- android 基于OkHttp GET 和 POST 的使用
- Android进阶之路 - okHttp中Get与Post初级使用(二)
- okhttp框架的最简单的基本使用 post和get请求以及用gson解析请求的数据
- android上面的OkHttp的简单get和post demo
- OkHttp 使用 get post UI线程回调 上传 下载 进度监听 更好地封装
- android使用HttpClient实现post get (+1)
- HttpUrlConnection的简单使用--get和post的简单使用
- OkHttp get post 简单用法
- Android使用http协议的GET和POST方法!!!
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)
- android_30_使用HttpClient发送Get和Post请求
- OkHttp的使用分析,okhttp的多种getpost方式
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- Android系列之网络(三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)