作者:琪琪格 | 来源:互联网 | 2023-08-29 20:08
okhttpGitHub地址:https:github.comsquareokhttp最新的OkHttp依赖可以到官方的GitHub上去添加。目前官方的依赖地址为&
okhttp GitHub地址:
https://github.com/square/okhttp
最新的OkHttp依赖可以到官方的GitHub上去添加。
目前官方的依赖地址为:
compile ‘com.squareup.okhttp3:okhttp:3.6.0’
Get请求
直接进入主题:
OkHttpClient okHttpClient=new OkHttpClient();Request.Builder builder=new Request.Builder();Request reqyest=builder.get().url("http://www.baidu.com").build();Call call=okHttpClient.newCall(reqyest);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {}@Overridepublic void onResponse(Call call, Response response) throws IOException {String res=response.body().string(); }});
最后在Manifests中打开网络权限即可使用Get请求请求网络数据了。
get请求头的格式:
例: url+”login?username=chengdu&password=123456”
POST请求
post请求大体步骤和GET请求差不多,具体如代码所示
//获得一个功能强大的FormBody表单RequestBody formBody=new FormBody.Builder().build()//2.构造RequestRequest.Builder builder=new Request.Builder()Request request=builder.url("http://www.imooc.com/").post(formBody).build()// 3、将Request封装为CallexecuteRequest(request)
RequestBody的数据格式都要指定Content-Type,常见的有三种:
- application/x-www-form-urlencoded 数据是个普通表单
- multipart/form-data 数据里有文件
- application/json 数据是个json
之后还要加上 charset=utf-8
POST Json例子
public void postJson(){RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),"{Json字符串}");Request.Builder builder=new Request.Builder();Request request=builder.url("http://www.imooc.com/").post(requestBody).build();executeRequest(request);}
POST 上传一个图片
public void postFile(){File file=new File(Environment.getExternalStorageDirectory(),"banana.jpg");if (!file.exists()){Log.e("-------","不存在");return;}RequestBody requestBody=RequestBody.create(MediaType.parse("application/octet-stream; charset=utf-8"),file);Request.Builder builder=new Request.Builder();Request request=builder.url("http://www.imooc.com/").post(requestBody).build();executeRequest(request);}
下载文件
public void downloadFile(){Request.Builder builder=new Request.Builder();Request request=builder.get().url("所要下载文件的文件路径").build();Call call=okHttpClient.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {Log.e("----","失败以后的方法");}@Overridepublic void onResponse(Call call, Response response) throws IOException {InputStream inputStream=response.body().byteStream();int len=0;File file=new File(Environment.getExternalStorageDirectory(), "zc.jpg");byte[] buf =new byte[128];FileOutputStream fos=new FileOutputStream(file);while ((len= inputStream.read(buf))!=-1){fos.write(buf,0,len);}fos.flush();fos.close();inputStream.close();}});
下载图片并且设置
public void downloadSetImage(){Request.Builder builder=new Request.Builder();Request request=builder.get().url("所要下载文件的文件路径").build();Call call=okHttpClient.newCall(request);call.enqueue(new Callback() {@Overridepublic void onFailure(Call call, IOException e) {Log.e("----","失败以后的方法");}@Overridepublic void onResponse(Call call, Response response) throws IOException {InputStream inputStream=response.body().byteStream();final Bitmap bitmap= BitmapFactory.decodeStream(inputStream);runOnUiThread(new Runnable() {@Overridepublic void run() {mImageView.setImageBitmap(bitmap);}});}});}