1.依赖(okhttp3里面是依赖于okio进行开发的,所以务必将okio也引入)
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.okio:okio:1.12.0'
2.权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
3.HttpUtils 封裝工具
package com.handsome.app4.Gallery;
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();
}
}
});
}
}
4.Post发送键值对演示:
HashMap<String, String> param=new HashMap<>();
param.put("code","admin");
param.put("password","bolesoft");
String url="http://192.168.200.211:8081/BLPDAService.asmx/GetUserInfo";
HttpUtils.doPost(url, param, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
MyLog.i("成功-------------",response.body().string());
}
});
以上均參考自