学习鸿洋的okhttp-utils,这边对各类请求方式作个总结:
具体见:hongyangAndroid/okhttputils okhttputilsgit
OkHttpUtils .get() .url("xxxxx") //项目须要就设置,不须要就不用写 .addHeader("Authorization", xxxxx) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(String response, int id, int code) { } });
OkHttpUtils .get() .url("xxxxx") .addHeader("Authorization", xxxxx) //添加参数 .addParams("xxxxx",xxxxx) .addParams("xxxxx", xxxxx) .build() //这里callback直接解析数据 //new GenericsCallback<你要解析的Bean类名> .execute(new GenericsCallback<FollowChangeListBean>(new JsonGenericsSerializator()) { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(FollowChangeListBean response, final int id, int code) { } });
##Postgithub
OkHttpUtils.post() .url("xxxxx") .addHeader("Authorization", "xxxxx") .addParams("xxxxx", "xxxxx") .build() .execute(new GenericsCallback<xxxxx>(new JsonGenericsSerializator()) { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(FollowChangeListBean.AddFollowListBean response, int id, int code) { } });
{ user_id:"" guests:[{ xxxx:"" }] }
JSONObject object = new JSONObject(); JSONArray array = new JSONArray(); array.put(xxxxx); try { object.put("user_id", xxxxx); object.put("guests",xxxxx); } catch (JSONException e) { e.printStackTrace(); } OkHttpUtils .postString() .url("xxxxx") .content(object.toString()) .mediaType(MediaType.parse("application/json;charset=utf-8")) .build() .execute(new GenericsCallback<DeviceConfig>(new JsonGenericsSerializator()) { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(DeviceConfig response, int id, int code) { } });
##DELETEweb
OkHttpUtils.delete() .url("xxxxx") .addHeader("Authorization", "xxxxx") .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(String response, int id, int code) { } });
JSONObject object = new JSONObject(); try { object.put("xxxxx", "xxxxx"); } catch (JSONException e) { e.printStackTrace(); } OkHttpUtils.delete() .url("xxxxx") .addHeader("Authorization", "xxxxx") .requestBody(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), object.toString())) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(String response, int id, int code) { } });
##PATCHjson
OkHttpUtils.patch() .url("xxxxx") .addHeader("Authorization","xxxxx") .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(String response, int id, int code) { } });
JSONObject object = new JSONObject(); try { object.put("xxxxx", "xxxxx"); object.put("xxxxx", "xxxxx"); } catch (JSONException e) { e.printStackTrace(); } OkHttpUtils.patch() .url("xxxxx") .addHeader("Authorization","xxxxx") .requestBody(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), object.toString())) .build() .execute(new StringCallback() { @Override public void onError(Call call, Exception e, int id, int code) { } @Override public void onResponse(String response, int id, int code) { } });