添加依赖
添加权限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
1.get请求获取慕课网首页数据
public void qcokhttpget(View view) { //get请求获取慕课网首页数据
Request request = new Request.Builder()
.get() //return method("GET", null);
.url("http://www.imooc.com/")
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("mydate" , "failure");
}
@Override
public void onResponse(Call call, Response response) throws IOException { //不在ui线程
String str = response.body().string();
Log.i("mydate" , str);
}
});
}
2.get方式下载图片
//okhttp的get方式下载图片
private void xiazaiwj() {
String url = "https://img-my.csdn.net/uploads/201603/26/1458988468_5804.jpg";
Request request = new Request.Builder() //Builder源码中默认method是GET
.url(url)
.build();
OkHttpClient okhttp = new OkHttpClient();
okhttp.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response){
InputStream inputstream = response.body().byteStream();
FileOutputStream fileoutputstream = null;
String filepath = "";
try{
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
} else{
filepath = getFilesDir().getAbsolutePath();
}
Log.i("mydate" , filepath);
File file = new File(filepath , "liuyan.jpg");
Log.i("mydate" , "bu wei kong");
fileoutputstream = new FileOutputStream(file);
byte[] buffer = new byte[2048];
int len = 0;
while((len = inputstream.read(buffer)) != -1){
fileoutputstream.write(buffer , 0 , len);
}
fileoutputstream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
3.post表单
//post表单
//post请求name=myliuyan age=22 并在eclipse的servlet中打印出来
//如果是写成get请求,则url= http://192.168.31.223:8080/MyServlet2/myserv?name=myliuyan&age=22
public void qcokhttppost(View view) {
RequestBody requestBody = new FormBody.Builder()
.add("name" , "myliuyan")
.add("age" , "22")
.build();
Request request = new Request.Builder()
.url("http://192.168.31.223:8080/MyServlet2/myserv")
.post(requestBody) //return method("post", null);
.build();
OkHttpClient okhttp = new OkHttpClient();
Call call = okhttp.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("mydate" , "失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.i("mydate" , str);
}
});
}
servlet: https://blog.csdn.net/qq_38261174/article/details/79662105 在这里可以知道如何写
4.post一个string
//post一个string
public void okhttppoststring(View view) {
//RequestBody.creat中的参数1代表类型,这里为string类型 , 第2参数表示类型对应的值.(上传文件,就是文件类型加文件)
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=utf-8") , "这只是一个string" );
Request request = new Request.Builder()
.url("http://192.168.31.223:8080/MyServlet2/myserv")
.post(requestBody) //post一个string
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("mydate" , "failure");
}
@Override
public void onResponse(Call call, Response response) throws IOException { //不在ui线程
String str = response.body().string();
Log.i("mydate" , str);
}
});
}
5.post一个file文件
//post一个file文件
public void okhttppostfile(View view) {
File file = new File(Environment.getExternalStorageDirectory() , "liuyan.jpg"); //SD卡储存上有这个文件 ,还要相关权限打开
if (!file.exists()){
Log.i("mydate" , "file not exist");
return;
}
//RequestBody.creat中的参数1代表类型,这里为(上传文件,就是 文件类型 加 文件 )
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream") , file );
Request request = new Request.Builder()
.url("http://192.168.31.223:8080/MyServlet2/myserv")
.post(requestBody) //post
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("mydate" , "failure");
}
@Override
public void onResponse(Call call, Response response) throws IOException { //不在ui线程
String str = response.body().string();
Log.i("mydate" , str);
}
});
}
6.这个服务端不知道写,所以不知道MultipartBody里面的东西怎么取出来。以后有时间再研究.
//post文件和相关参数,同时上传
public void okhttppostfileandparams(View view) {
File file = new File(Environment.getExternalStorageDirectory() , "liuyan.jpg"); //SD卡储存上有这个文件 ,还要相关权限打开
if (!file.exists()){
Log.i("mydate" , "file not exist");
return;
}
//RequestBody.creat中的参数1代表类型,这里为(上传文件,就是 文件类型 加 文件 )
//RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream") , file );
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("name" , "liuyan")//AA
.addFormDataPart("age" , "22") //BB
.addFormDataPart("image" , "my.jpg" , RequestBody.create(MediaType.parse("application/octet-stream") , file ))
.build(); //AAaddFormDataPart的第一个参数为key,第2参数为value
//BBaddFormDataPart的第一个参数为key,第二个参数为filename(value),第三个参数为requestbody(文件,json,string等)
//key在eclipse的servlet中必须要一致
Request request = new Request.Builder()
.url("http://192.168.31.223:8080/MyServlet2/myserv")
.post(requestBody) //post
.build();
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("mydate" , "failure");
}
@Override
public void onResponse(Call call, Response response) throws IOException { //不在ui线程
String str = response.body().string();
Log.i("mydate" , str);
}
});
}