zoukankan      html  css  js  c++  java
  • OkHttp3 + retrofit2 封装

    0.下载文件

    1.gradle 添加

    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.okio:okio:1.9.0'

    2.AndroidManifest.xml 权限   

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    3.Application中初始化 

    HttpUtil.getInstance().init(this, "http://baiqi.ej-cloud.com:8070/iot/");

    4.调用

                    HttpUtil.getInstance().get(
                            new HttpUtil.Builder()
                                    .url("user/token/init")
                                    .params("key", "1")
                                    .params("type", "4")
                                    .callBackSuccess(new CallBackSuccess() {
                                        @Override
                                        public void onSuccess(String json) {
                                            TopicResponse topicResponse = JsonUtil.fromJson(json, TopicResponse.class);
                                            tv.setText(topicResponse.data.id);
                                        }
                                    }));

     5.添加上传图片功能 HttpUtil.java文件中

    private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
        public void uploadImages(List<String> selectedImages, String url) {
            // mImgUrls为存放图片的url集合
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            for (int i = 0; i < selectedImages.size(); i++) {
                File f = new File(selectedImages.get(i));
                if (f != null) {
                    builder.addFormDataPart("img" + i, f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
                }
            }
    
            MultipartBody requestBody = builder.build();
            //构建请求
            Request request = new Request.Builder()
                    .url(url)//地址
                    .post(requestBody)//添加请求体
                    .build();
            okHttpClient.newCall(request).enqueue(new okhttp3.Callback() {
                @Override
                public void onFailure(okhttp3.Call call, IOException e) {
                    Log.wtf(TAG, "request " + call.request().url());
                    Log.wtf(TAG, "上传失败:e.getLocalizedMessage() = " + e.getLocalizedMessage());
                }
    
                @Override
                public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                    Log.wtf(TAG, "request " + call.request().url());
                    Log.wtf(TAG, "上传照片成功:response = " + response.body().string());
                }
            });
        }

    调用  

    HttpUtil.getInstance().uploadImages(selectedImages, "http://baiqi.ej-cloud.com/iotlife/user/opinion/imgUpload&key=1&image=uicon&token="
                   +token+"&feedbackId" +feedbackId );

    参考:

    https://github.com/gengqiquan/HttpUtil

    https://github.com/hongyangAndroid/okhttputils

  • 相关阅读:
    2017.3.11[bzoj2440][中山市选2011]完全平方数
    2017.3.6[hihocoder#1415]后缀数组三·重复旋律3
    2017.3.4[hihocoder#1407]后缀数组二·重复旋律2
    [NOI2013]快餐店
    [HNOI2014]米特运输
    [HNOI2015]亚瑟王
    [JLOI2013]卡牌游戏
    [SDOI2010]地精部落
    [ZJOI2007]棋盘制作
    [AHOI2009]中国象棋
  • 原文地址:https://www.cnblogs.com/Westfalen/p/6764059.html
Copyright © 2011-2022 走看看