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

okhttp

2016-06-14 10:12 459 查看
jar包
https://yunpan.cn/cRcDaaJbfJgdd  访问密码 ca91

 

get请求

//创建okHttpClient对象
OkHttpClient okClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
.url("http://www.quwenlieqi.com/app/v2/api.php?m=102")
.build();
//new call
Call call = okClient.newCall(request);
//请求加入调度
call.enqueue(new Callback()
{
@Override
public void onFailure(Request request, IOException e)
{
System.out.println("请求失败");
}

@Override
public void onResponse(final Response response) throws IOException
{
System.out.println("请求成功");
String htmlStr =  response.body().string();
System.out.println(htmlStr);
                

                 //返回二进制字节数组

                 //byte[] bytes = response.body().bytes();

                 //返回流inputStream

                 //InputStream is = response.body().byteStream();

}
});
}


 post请求

// 创建okHttpClient对象
OkHttpClient okClient = new OkHttpClient();
FormEncodingBuilder builder = new FormEncodingBuilder();
//        builder.add("cid","27");
//        builder.add("num","20");

Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
// new call
Call call = okClient.newCall(request);
// 请求加入调度
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
System.out.println("请求失败");
}

@Override
public void onResponse(final Response response) throws IOException {
System.out.println("请求成功");
//返回字符串
String htmlStr = response.body().string();
System.out.println(htmlStr);

//返回二进制字节数组
//byte[] bytes = response.body().bytes();

//返回流inputStream
//InputStream is = response.body().byteStream();
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: