跳至主要内容

HttpToolKit java android http OkHttp wrapper

class HttpToolKit{
    static void PostMe(String str, HashMap<String,String> form,HashMap<String,String> files,Callback callback)throws Exception{
        if (!str.startsWith("http://")){
            str="http://"+str;
        }
        String boundary = "xx--------------------------------------------------------------xx";
        MultipartBody.Builder builder = new MultipartBody.Builder(boundary).setType(MultipartBody.FORM);
        if (files!=null&&files.size()>0){
            Iterator<Map.Entry<String, String>> it = files.entrySet().iterator();
            while (it.hasNext()){
                HashMap.Entry<String,String> entry=it.next();
                String[] strs=entry.getValue().split("/");
                String filename=strs[strs.length-1];
                builder.addFormDataPart(entry.getKey(),filename,RequestBody.create(MediaType.parse("application/octet-stream"),new File(entry.getValue())));
            }
        }
        if (form!=null&&form.size()>0){
            Iterator<HashMap.Entry<String,String>> it=form.entrySet().iterator();
            while (it.hasNext()){
                HashMap.Entry<String,String> entry=it.next();
                builder.addFormDataPart(entry.getKey(),entry.getValue());
            }
        }
        Request request = new Request.Builder().url(str).post(builder.build()).build();
        OkHttpClient client=new OkHttpClient();
        client.newCall(request).enqueue(callback);
    }
}

评论

此博客中的热门博文

android hide actionbar

public class MainActivity extends Activity { ActionBar actionBar; //声明ActionBar @Override protected void onCreate( Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( R .layout.activity_main); actionBar = getSupportActionBar(); //得 到ActionBar actionBar.hide(); //隐藏ActionBar } }

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.