抽空封装了一下基于okHttp的网络请求,刚刚封装好get,post, download,upload请求,不需要创建线程,支持Json自动转换 。
Get和Post(文件上传也属于Post)主要分俩个步骤:
1.通过HttpRequestBuilder来创建请求实体对象。
2.通过HttpUtil的sendRequest方法来发送请求,并且使用ResultCallBack来接受返回内容,这个步骤中成功时,自动将json转换为实体,若失败会执行ResultCallBack的onError 方法,并传递HttpException,这个对象中包含http错误代码,比如404,500等。
get请求方式:
private void getSample() { HttpRequest request = new HttpRequestBuilder().url(url) .addHeader("cookie", "df").addParams("key", "value") .method(HttpMethod.GET).build(); HttpUtil.getInstance().sendRequest(request, new ResultCallBack<Result>() { @Override public void onResponse(Result response) { // TODO Auto-generated method stub textView.setText(response.getData().getName()); } @Override public void onError(Request request, HttpException httpException) { // TODO Auto-generated method stub textView.setText("requestCode:"+httpException.getResponseCode()+" ErrorMessage:"+httpException.getMessage()); } }); }
post请求:
private void postSample() { HttpRequest request = new HttpRequestBuilder() .url("http://www.oschina.net/action/user/hash_login") .addHeader("cookie", "df").addParams("email", "xxxxxxxxx") .addParams("pwd", "xxxxxxxxxxxxxxxxx") .method(HttpMethod.POST).build(); HttpUtil.getInstance().sendRequest(request, new ResultCallBack<String>() { @Override public void onError(Request request, HttpException httpException) { // TODO Auto-generated method stub } @Override public void onResponse(String response) { // TODO Auto-generated method stub textView.setText(response); } }); }
文件上传:
private void fileUploadSample() { if (!TextUtils.isEmpty(textView.getText())) { File file = new File(textView.getText().toString()); final String url = "服务器地址。。。"; HttpRequest request = new HttpRequestBuilder() .url(url) .method(HttpMethod.POST) .addFile( new FileDiscription(file) .setUploadPrograssListener(fileUploadListener)) .build(); HttpUtil.getInstance().sendRequest(request, new ResultCallBack<String>() { @Override public void onError(Request request, HttpException httpException) { // TODO Auto-generated method stub } @Override public void onResponse(String response) { // TODO Auto-generated method stub textView.setText(response); } }); } }
文件下载是Get请求,只不过没有使用ResultCallBack而是使用了DownloadCallBack,可以监听下载进度,有完成和失败的回调。代码如下:
private void downLoad(){ HttpRequest request= new HttpRequestBuilder() .url("http://img.taopic.com/uploads/allimg/130501/240451-13050106450911.jpg") .method(HttpMethod.GET).downLoadPath(filePath, String.valueOf(new Date().getTime())+".jpg").build(); HttpUtil.getInstance().downLoad(request, new DownLoadCallBack() { @Override public void onDownLoading(int prograss) { // TODO Auto-generated method stub textView.setText(prograss+"%"); progressBar.setProgress(prograss); } @Override public void onDownLoadSuccess() { // TODO Auto-generated method stub textView.setText("down load success"); } @Override public void onDownLoadFail(HttpException ex) { // TODO Auto-generated method stub textView.setText(ex.getMessage()); } }); }
源码地址:
https://github.com/YounMario/HttpDemo
时间: 2024-10-16 20:16:19