android 使用Okhttp封装上传JSON格式工具类

public class PostRequestJSON_Util {    private static PostRequestJSON_Util postRequestJSONUtil;    private Handler mHandler = new Handler(Looper.getMainLooper());    public static PostRequestJSON_Util getInstance() {        if (postRequestJSONUtil == null) {            postRequestJSONUtil = new PostRequestJSON_Util();        }        return postRequestJSONUtil;    }    //    map集合,需要上传的josn数据    public void postJson(String url, Map map, setResponseListener mListener) {        String jsonStr = new Gson().toJson(map);        RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonStr);        OkHttpClient client = new OkHttpClient();        Request request = new Request.Builder()                .url(url)                .post(body)                .build();        client.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                if (mListener != null) {                    mHandler.post(new Runnable() {    //使用handler切换到主线程更新UI                        @Override                        public void run() {                            try {                                mListener.onFailers(e.getMessage());     //接口回调失败方法                            } catch (Exception e) {                                e.printStackTrace();                            }                        }                    });                }            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if (response.isSuccessful()) {                    if (mListener != null) {                        mHandler.post(new Runnable() {                            @Override                            public void run() {                                try {                                    mListener.onSuccessful(response);    //接口回调成功方法                                } catch (Exception e) {                                    e.printStackTrace();                                }                            }                        });                    }                }            }        });    }    public interface setResponseListener {                void onSuccessful(Response response);        void onFailers(String str);    }}

使用如下:

//                String URL = "https://api.test.ndmooc.com/v1/digitalmeet/add?token=9dbeb5da0d7372596e363a9383ea91aff855f18d";                String URL = API.apiurl() + "v1/digitalmeet/add?token=" + token;                HashMap map = new HashMap<>();                map.put("mynotebooks_id", mynotebook_id);                map.put("title", mTvMeetTitle.getText().toString());                if (mTvType.getText().toString().equals("会议")) {                    map.put("type", "1");                } else if (mTvType.getText().toString().equals("学习")) {                    map.put("type", "2");                }                PostRequestJSON_Util.getInstance().postJson(URL, map, new PostRequestJSON_Util.setResponseListener() {                    @Override                    public void onSuccessful(Response response) {                        try {                            progressDialog.dismiss();                            String string = response.body().string();                            NewBuildBean buildBean = new Gson().fromJson(string, NewBuildBean.class);                            digitalmeet_id = buildBean.getData().getDigitalmeet_id();                            startActivity(new Intent(NewBuildMeetActivity.this, MeetingMessageActivity.class).putExtra("digitalmeet_id",                                    digitalmeet_id).putExtra("mynotebook_id",                                    mynotebook_id).putExtra("notebook_id", notebook_id));                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                    @Override                    public void onFailers(String str) {                        Log.i(TAG, "onFailers: " + str);                    }                });

 

更多相关文章

  1. android获取sd卡路径方法:
  2. android中常见的二种数据解析方法----XML和Json
  3. Android SDK Manager无法更新解决方法
  4. Android Timer 更好方法
  5. android调用系统相机并调整照片大小保存,最后上传照片
  6. 删除android ScrollView边界阴影方法
  7. Android base64 上传图片
  8. Android显示网络图片相关实现方法浅谈

随机推荐

  1. Android(安卓)library projects cannot b
  2. android 判断网络时打开设置
  3. Browser设置为桌面版
  4. android下的android.os.StrictMode$Andro
  5. 如何让listview宽度自适应
  6. 2010.12.08——— android progressDialo
  7. Android(安卓)去掉状态栏 去掉标题栏 沉
  8. wifi
  9. Android(安卓)呼吸灯流程分析(二)
  10. Android(安卓)WebView访问SSL证书网页(onR