OkHttpUtils
2016-08-12 12:00
295 查看
OkHttpUtils工具类
在类中调用
package com.example.eggplant_doctor.util; import com.squareup.okhttp.Call; import com.squareup.okhttp.FormEncodingBuilder; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; import java.io.IOException; import java.util.Map; /** * Created by Administrator on 2016/6/14. */ public class Okutil { private static OkHttpClient client; public static String getGet(String url) { //创建okHttpClient对象 OkHttpClient mOkHttpClient = new OkHttpClient(); //创建一个Request final Request request = new Request.Builder() .url(url) .build(); //new call Call call = mOkHttpClient.newCall(request); //请求加入调度 try { Response response = call.execute(); return response.body().string(); } catch (IOException e) { e.printStackTrace(); } return null; } public static String getGetGbk(String url) { //创建okHttpClient对象 OkHttpClient mOkHttpClient = new OkHttpClient(); //创建一个Request final Request request = new Request.Builder() .url(url) .build(); //new call Call call = mOkHttpClient.newCall(request); //请求加入调度 try { Response response = call.execute(); return new String(response.body().bytes(),"gbk"); } catch (IOException e) { e.printStackTrace(); } return null; } public static String getPost(String url, Map<String, String> data) throws IOException { client = new OkHttpClient(); FormEncodingBuilder formBuilder = new FormEncodingBuilder(); for (Map.Entry<String, String> item : data.entrySet()) { formBuilder.add(item.getKey(), item.getValue()); /* Log.i("key", item.getKey()); Log.i("values", item.getValue());*/ } RequestBody body = formBuilder.build(); Request request = new Request.Builder().url(url).post(body).build(); Response response = excute(request); if (response.isSuccessful()) { return response.body().string(); } else { throw new IOException("Unexpected code " + response); } } private static Response excute(Request request) throws IOException { return client.newCall(request).execute(); } }
在类中调用
new Thread(new Runnable() { @Override public void run() { String get = Okutil.getGet("http://www.hengboit.com/json/CourseInformation.json"); getActivity().runOnUiThread(new Runnable() { @Override public void run() { Gson g=new Gson(); } }); } }).start();
相关文章推荐
- OKHttpUtils
- okhttp封装okhttp-utils
- okhttp-utils使用
- 开源框架OKHttp的使用02-封装后的OkHttpUtils
- OkHttpUtils | okhttp-OkGo的使用,完美支持RxJava
- OkHttpUtils-2.0.0 升级后改名 OkGo,全新完美支持RxJava,比Retrofit更简单易用
- OkHttpUtils
- Android——网络框架篇:OkHttpUtils一个专注于让网络请求更简单的框架
- OkHttpUtils - 封装了okhttp的网络框架
- Android okHttp封装库(1) -- okhttp-utils
- OkhttpUtils传body
- okhttputils【 Android 一个改善的okHttp封装库】使用(二)
- OkHttpUtils - 封装了okhttp的网络框架
- OKhttpUtils
- OkHttpUtils进一步的封装
- 封装的OkHttpUtils,拦截器,gsonutils类,解绑(防止内内存泄漏)
- 基于okhttp3的工具类HttpUtils
- OkHttpUtils
- Okhttp与Okhttputils的用法及区别
- OkHttpUtils的常规使用