上面是获取OkhttpUtils设置获取http连接前后回调类的方法,下面才是真正使用OKhttpUtils的方法

//get请求文本
public void getHttpText() throws Exception {
OkHttpUtils
.get()
.url("http://www.baidu.com")
.addParams("username", "heyman")
.addParams("password", "123")
.id(20)
.build()
.execute(new MyStringCallback());
}
//post提交文本
public void postHttpText(){
String url = "http://www.baidu.com";
OkHttpUtils
.post()
.url(url)
.id(100)
.build()
.execute(new MyStringCallback());
}
//下载文件
public void downloadFile(){
OkHttpUtils//
.get()//
.url("http://vfx.mtime.cn/Video/2016/07/24/mp4/160724055620533327_480.mp4")//
.build(http://www.amjmh.com/v/BIBRGZ_558768/)//
.execute(new FileCallBack(Environment.getExternalStorageDirectory().getAbsolutePath(),"MyMusic.mp4"){
@Override
public void onError(Call call, Exception e, int i) {
}
@Override
public void onResponse(File file, int i) {

}
@Override
public void inProgress(float progress, long total, int id) {
super.inProgress(progress, total, id);
progressBar.setProgress((int) (100 * progress));
}

});
}
?上传文件还需要一个已经编写好的Servlet的war文件来做测试,将他放入tomcat服务器的webapps目录下,启动tomcat服务器
————————————————

原文地址:https://www.cnblogs.com/hyhy904/p/11402888.html