首页 Android OkHttp3封装类与调用方法祥解

OkHttp3封装类与调用方法祥解

作者:胡同里的砖头 围观群众:289 更新于:2022-12-06

HttpUtils.java

package com.example.bysj.util;

import android.widget.Toast;

import com.example.bysj.LoginActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class HttpUtils {

private static OkHttpClient client = null;

private HttpUtils() {}

public static OkHttpClient getInstance() {
if (client == null) {
synchronized (HttpUtils.class) {
if (client == null)
client = new OkHttpClient();
}
}
return client;
}

/**
* Get请求
*
* @param url
* @param callback
*/
public static void doGet(String url, Callback callback) {
Request request = new Request.Builder()
.url(url)
.build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}

/**
* Post请求发送键值对数据
*
* @param url
* @param mapParams
* @param callback
*/
public static void doPost(String url, Map<String, String> mapParams, Callback callback) {
FormBody.Builder builder = new FormBody.Builder();
for (String key : mapParams.keySet()) {
builder.add(key, mapParams.get(key));
}
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}

/**
* Post请求发送JSON数据
*
* @param url
* @param jsonParams
* @param callback
*/
public static void doPost(String url, String jsonParams, Callback callback) {
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8")
, jsonParams);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}

/**
* 上传文件
*
* @param url
* @param pathName
* @param fileName
* @param callback
*/
public static void doFile(String url, String pathName, String fileName, Callback callback) {
//判断文件类型
MediaType MEDIA_TYPE = MediaType.parse(judgeType(pathName));
//创建文件参数
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(MEDIA_TYPE.type(), fileName,
RequestBody.create(MEDIA_TYPE, new File(pathName)));
//发出请求参数
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + "9199fdef135c122")
.url(url)
.post(builder.build())
.build();
Call call = getInstance().newCall(request);
call.enqueue(callback);

}

/**
* 根据文件路径判断MediaType
*
* @param path
* @return
*/
private static String judgeType(String path) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String contentTypeFor = fileNameMap.getContentTypeFor(path);
if (contentTypeFor == null) {
contentTypeFor = "application/octet-stream";
}
return contentTypeFor;
}

/**
* 下载文件
* @param url
* @param fileDir
* @param fileName
*/
public static void downFile(String url, final String fileDir, final String fileName) {
Request request = new Request.Builder()
.url(url)
.build();
Call call = getInstance().newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
File file = new File(fileDir, fileName);
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();
}
}
});
}
}

调用演示
/*
* Get请求
* 参数一:请求Url
* 参数二:请求回调
*/
HttpUtils.doGet("https://www.so.com/", new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()){
tv.setText(response.body().string());
}
//关闭防止内存泄漏
if(response.body()!=null){
response.body().close();
}
}
});


/*
* Post请求
* 参数一:请求Url
* 参数二:请求的键值对
* 参数三:请求回调
*/
Map<String,String> map = new HashMap<>();
map.put("username","Hensen");
map.put("password","123456");

HttpUtils.doPost("http://192.168.31.109:8080/test.php", map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {

}
});


/*
* Post请求
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
String json = "{\"name\":\"Hensen\"}";

HttpUtils.doPost("http://192.168.0.8:8080/test.php", json, new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {

}
});


/*
* 上传文件
* 参数一:请求Url
* 参数二:保存文件的路径名
* 参数三:保存文件的文件名
* 参数四:请求回调
*/
HttpUtils.doFile("http://192.168.0.8:8080/WD/admin.php?a=ajaxuploadimage&&c=Image",
"/data/data/com.handsome.app4/logoa.jpg", "logoa.jpg", new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {

}
});


/*
* 下载文件
* 参数一:请求Url
* 参数二:保存文件的路径名
* 参数三:保存文件的文件名
*/
HttpUtils.downFile("http://shouji.360tpcdn.com/160804/a05b75b7779f7a4afae83601c195ed2b" +
"/com.qihoo.haosou_708.apk"
,"/data/data/com.handsome.app4/","aqy.apk");

  • 本文标题: OkHttp3封装类与调用方法祥解
  • 文章分类:【Android】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8