1需要okhttp-2.5.0.jar和okio-1.6.0.jar的支持
下载地址:
http://download.csdn.net/detail/zhaihaohao1/9470796
2开网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
3核心代码
get请求(异步)
<span style="font-size:18px;"> /**
* 异步请求
* 发送网络请求
* get请求
* 默认已经把线程分装好了
*/
private void myHttpGet() {
String url = "http://112.124.22.238:8081/course_api/banner/query?type=1";
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
httpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String jsonStr = response.body().string();
Log.e("1", jsonStr);
}
}
}
);
}
</span>
post请求(异步)
<span style="font-size:18px;"> /**
* 异步请求
* 发送网络请求
* post请求
* 默认已经把线程分装好了
*/
private void myHttpPost() {
String url = "http://112.124.22.238:8081/course_api/banner/query";
OkHttpClient httpClient = new OkHttpClient();
// 参数
RequestBody body = new FormEncodingBuilder()
.add("type", "1")
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
httpClient.newCall(request).enqueue(new com.squareup.okhttp.Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String jsonStr = response.body().string();
Log.e("1", jsonStr);
}
}
});
}
</span>
post请求上传json字符串(异步)
public void httpjson(){
String url = "http://192.168.1.124:7775/mobile/applyRsJb/insert";
String json= "{\"createUserId\":\"17\",\"departmentId\":\"4\",\"reason\":\"ghhjgg\",\"startTime\":\"2019-04-04 15:32:30\",\"stopTime\":\"2019-04-04 17:32:32\"}";
MediaType JSON=MediaType.parse("application/json;charset=utf-8");
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(JSON, json);
//创建一个请求对象
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.i("failed", e.toString());
}
@Override
public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
String string = response.body().string();
Logger.t("111").d(string);
} else {
Logger.t("111").d("数据请求错误");
}
}
});
}
多图片上传:(异步)
//参数类型
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/jpg");
//创建OkHttpClient实例
private final OkHttpClient client = new OkHttpClient();
private void uploadImage2(){
if(allPath.size()!=0) {
Log.e("1","allPath中的内容"+allPath);
String url=HttpUrl.mobileUploadImgFile;
MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
// //遍历map中所有参数到builder
// for (String key : map.keySet()) {
// builder.addFormDataPart(key, map.get(key));
// }
//遍历paths中所有图片绝对路径到builder,并约定key如“upload”作为后台接受多张图片的key
//图片的名字
for (String path : allPath) {
File file=new File(path);
Log.e("1","file.getName():"+file.getName());
builder.addFormDataPart("upload", file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file));
}
//构建请求体
RequestBody requestBody = builder.build();
//构建请求
Request request = new Request.Builder()
.url(url)//地址
.post(requestBody)//添加请求体
.build();
//发送异步请求,同步会报错,Android4.0以后禁止在主线程中进行耗时操作
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.e("1","request ="+ request.urlString());
Log.e("1","e.getLocalizedMessage() = "+e.getLocalizedMessage());
}
@Override
public void onResponse(Response response) throws IOException {
//看清楚是response.body().string()不是response.body().toString()
Log.e("1","response = " + response.body().string());
}
});
}
}
源码下载:
http://download.csdn.net/detail/zhaihaohao1/9470870