okhttp简单的应用
2015-12-04 17:09
579 查看
okhttp官网:http://square.github.io/okhttp/
okhttp的git仓库:https://github.com/square/okhttp
okhttp的使用方法:https://github.com/square/okhttp/wiki/Recipes
一、配置
android studio中,在app下的build.gradle文件中添加:compile ‘com.squareup.okhttp:okhttp:2.6.0’(2.6.0为版本号,最好写最新的),如下:
二、Get方法(获取数据):
看代码
三、下载图片:
四、Post方法:
五、上传图片或文件
上传文件或图片是的进度条,请参考:1、http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/djk_dong/article/details/48179315/
2、http://blog.csdn.net/sbsujjbcy/article/details/48194701
okhttp的git仓库:https://github.com/square/okhttp
okhttp的使用方法:https://github.com/square/okhttp/wiki/Recipes
一、配置
android studio中,在app下的build.gradle文件中添加:compile ‘com.squareup.okhttp:okhttp:2.6.0’(2.6.0为版本号,最好写最新的),如下:
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.squareup.okhttp:okhttp:2.6.0' }
二、Get方法(获取数据):
看代码
/** * okhttp Get方法获取数据 * @param url * @return */ public static void getData(String url){ try { //请求 Request request = new Request.Builder() .url(url) .build(); /** * 方法一 * 同步的 */ //Response response = okHttpClient.newCall(request).execute(); // // //判断是否请求成功 // if (!response.isSuccessful()) throw new Exception("Unexpected code:"+response); // // Headers headers = response.headers(); // for (int i = 0;i<headers.size();i++){ // System.out.println("Get请求结果:"+headers.name(i)+":"+headers.value(i)); // } // // System.out.println("Get请求结果(字符串):"+response.body().toString()); /** * 方法二 * 异步的,即enqueue是运行在子线程中的,回调函数Callback()中 * 的两个方法也是运行在子线程的 */ okHttpClient.newCall(request).enqueue(new Callback() { //此方法是在用户取消此操作、请求超时等情况下调用 @Override public void onFailure(Request request, IOException e) { e.printStackTrace(); } //此方法是在成功地从服务端请求到了数据时调用 @Override public void onResponse(Response response) throws IOException { //在此对response进行处理 //判断是否请求成功 if (!response.isSuccessful()) throw new IOException("Unexpected code:"+response); Headers headers = response.headers(); for (int i = 0;i<headers.size();i++){ System.out.println("Get请求结果:"+headers.name(i)+":"+headers.value(i)); } System.out.println("Get请求结果(字符串):"+response.body().toString()); } }); }catch (Exception e){ e.printStackTrace(); } }
三、下载图片:
/** * 下载图片是得到图片的字节数组,然后在转换成bitmap */ private static byte[] getBytesData(String url) throws Exception { Request request = new Request.Builder() .url(url) .build(); Response response = okHttpClient.newCall(request).execute(); byte[] imageBytes = response.body().bytes(); return imageBytes; }
四、Post方法:
/** * Post方法就是比Get方法多了一个post(requestBody),多一个 * RequestBody * @param url * @param jsonStr jsonStr是通过JSONStringer构成的JSON格式 * 的数据,然后转换成字符串 */ private static String getJsonData(String url, String jsonStr) throws Exception { RequestBody body = RequestBody.create(JSON, jsonStr); Request request = new Request.Builder() .url(url) .post(body) .build(); Response response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { String s = response.body().string(); //请求的结果 return s; } else { throw new IOException("Unexpected code " + response); } }
五、上传图片或文件
/** * 上传图片 * @param url 上传图片的地址url * @param path 手机中图片或文件的路径 */ private static String uploadPicture(String url, String path) throws Exception { File file; file = new File(path); //构造请求体 RequestBody requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addFormDataPart("file", file.getName(), RequestBody.create(MEDIA_TYPE, file)) .build(); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); Response response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()) { String s = response.body().string(); //上传图片的结果 return s; } else { throw new IOException("Unexpected code " + response); } }
上传文件或图片是的进度条,请参考:1、http://46aae4d1e2371e4aa769798941cef698.devproxy.yunshipei.com/djk_dong/article/details/48179315/
2、http://blog.csdn.net/sbsujjbcy/article/details/48194701
相关文章推荐
- HTTP状态码(HTTP Status Code)及常用场景
- C# HttpWebRequest 绝技 转至 http://www.sufeinet.com/
- 虚电路与数据包网络比较(计算机网络学习笔记)
- Redhat 7.0使用CentOS 7 的Yum 网络源
- linux网络配置和工具
- 企业网站建设 企业网络信息化不可怀疑的力量
- httpd配置文件常用参数详解
- 第8章 网络安全
- java网络编程一
- [osgi] 将OSGI嵌入到HTTP服务器中去
- c++实现http协议模仿get方法访问tomcat
- 五款超级易用的网络监控软件
- 中继器、集线器、网桥、交换机、路由器和网关在网络协议中工作的层次(计算机网络学习笔记)
- Andrew Ng机器学习入门学习笔记(四)之神经网络(二)
- OkHttp get post 简单用法
- iOS代码技巧之ASIHttpRequest类库的使用及Json解析(以解析中国天气网天气预报为例)
- 如何解决在HttpURLConnection的getInputStream方法调用时出错的问题
- use c to send file using http post
- js原生创建封装ajax的方法,附XMLHttpRequest状态码
- iOS开发之XCode模拟器不能连接网络