向服务器上传file流的工具类

okhttp上传file 流的工具类:

package listview.mifeng.us.jianbao.webutil;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.google.gson.Gson;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import listview.mifeng.us.jianbao.toastutils.ShowToastUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.R.attr.handle;
import static android.content.ContentValues.TAG;


/**
 * Created by 孙贝贝 on 2016/12/1.
 */

public class PhotoPostUtil {
    public static   GetRegisterData data;
    private  static  final  Handler handler = new Handler();
    public interface GetRegisterData{
        void setRegisterData(Object o);
    }
    public static  void  getData(GetRegisterData data1){
         data= data1;
    }
    private static  final Gson gson= new Gson();
    private static  final OkHttpClient client= new OkHttpClient();
    private static MultipartBody.Builder requestBody;

    public static void upLoad(File file, final Context context, String  path, HashMap<String ,String>map, final Type clas){
        RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
        requestBody = new MultipartBody.Builder();
        requestBody.setType(MultipartBody.FORM);
        //"card"参数类型 file.getName 获取文件名
        requestBody.addFormDataPart("card",file.getName(),fileBody);

        Set<Map.Entry<String ,String>>entries= map.entrySet();
        for (Map.Entry<String,String>entry:entries){
            requestBody.addFormDataPart(entry.getKey(),entry.getValue());
        }
        MultipartBody build = requestBody.build();
        Request request = new Request.Builder()
                .url(path)
                .post(build)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ShowToastUtils.showToast(context,"上传数据失败");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Object o = gson.fromJson(string, clas);
                        data.setRegisterData(o);

                    }
                else
                    {
                        Log.e(TAG, "onResponse: "+response.code());
                    }
            }
        });


    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值