我这里主要是介绍了okhttp的两个请求方式,首先呢我们要导入jar包,
--------使用Ardroid Studio的用户直接在dependencies中加入:
compile 'com.squareup.okhttp3:okhttp:3.4.1'
--------使用ADT的用户 ,可以下载最新的 okhttp he latest JAR ,添加依赖就可 以了。
注意:okhttp内部依赖okio,别忘了同时导入okio:gradle:
compile 'com.squareup.okio:okio:1.5.0'
最新的jar地址:okio the latest JAR
那我下面我直接上代码,演示get和post两种请求方式,里面都做了详细的注释:
MainActivity类:PostData类public class MainActivity extends AppCompatActivity { private String urlpath = "http://upload.ybxww.com/2016/0831/1472626158930.png"; private String path = "http://japi.juhe.cn/tv/getCategory"; private ImageView imageview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview = (ImageView) findViewById(R.id.imagview); getdata(); postData(); } private void postData() { Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { PostData.getPost(path, "key", "32d6fd626e96bc714744a2fc43aae475"); } }); } private void getdata() { Executors.newSingleThreadExecutor().execute(new Runnable() { @Override public void run() { final Bitmap data = GetData.getData(urlpath); runOnUiThread(new Runnable() { @Override public void run() { imageview.setImageBitmap(data); } }); } }); } }
GetData类public class PostData { private static final String TAG = "PostData"; private static String name; private static ArrayList<String> list; public static ArrayList<String> getPost(String path, String key, String value) { //创建一个OkHttpClient的客户端对象 OkHttpClient client = new OkHttpClient(); //拿到FormBody的一个对象,通过Builder来添加他的key和value,得到一个body FormBody body = new FormBody.Builder() .add(key, value) .build(); //创建一个请求对象,通过Builder把网址携带到url,把body值携带到post Request request = new Request.Builder() .url(path) .post(body) .build(); //通过OkHttpClient的对象new出一个调用和访问的对象,得到一个call Call call = client.newCall(request); try { list = new ArrayList<String>(); //然后执行这个访问得到一个Response的响应对象 Response response = call.execute(); //通过Response对象拿到一个状态码 int code = response.code(); //判断这个码儿是否等于200,等于200说明联网成功 if (code == 200) { //ResponseBody相当于拿到一个response的实体 ResponseBody body1 = response.body(); //把这个实体转换成字节 byte[] bytes = body1.bytes(); //在转换成字符串 String s = new String(bytes); Log.e(TAG, "getPost: " + s); /* 解析部分不多说 这里只是控制台打印了一下数据,没有做过多的逻辑 */ JSONObject ob = new JSONObject(s); try { JSONArray array = ob.getJSONArray("result"); for (int i = 0; i < array.length(); i++) { JSONObject obs = (JSONObject) array.get(i); name = obs.getString("name"); list.add(name); } Log.i("tag", list.size() + ""); } catch (JSONException e) { e.printStackTrace(); } } else { Log.i("tag", "下载失败"); } } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return list; } }
public class GetData { private static Bitmap bitmap; public static Bitmap getData(String ss) { /* 其实post请求搞懂了,get也就会了,下面就不一一写注释了,对比post看一下 只是不需要FormBody对象了 */ OkHttpClient client = new OkHttpClient(); Request re = new Request.Builder() .url(ss) .get() .build(); Call call = client.newCall(re); try { Response response = call.execute(); if (response.code() == 200) { ResponseBody body = response.body(); byte[] bytes = body.bytes(); bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } } catch (IOException e) { e.printStackTrace(); } return bitmap; } }
好,这样就完成了简单的OkHttpClient的get和post的两种联网操作,这只是简单的,希望大家也多多学习,往深入了解一番,会有很大的发现!!