• xutils 上传文件 ,暂时



    public class ApiClient {
    static final String LOG_TAG="ApiClient";
    private static HttpUtils defaultHttpClient=null; //实例话对象


    public static HttpUtils getDefaultHttpClient(){
    // if(defaultHttpClient==null){
    defaultHttpClient = new HttpUtils();
    //设置连接超时时间
    defaultHttpClient.configTimeout(2000);
    //配置socket时间连接溢出
    defaultHttpClient.configSoTimeout(2000);
    // }

    return defaultHttpClient;
    }

    /**
    * 提交Get请求
    * */
    public static void sendGet(ApiAction action){
    Logger.d(LOG_TAG, "@@@@@Get=Url: " + action.getUrl());
    //检查联网状态,允许联网才提交请求。
    try{
    HttpUtils defaultHttpClient = getDefaultHttpClient();
    defaultHttpClient.send(HttpRequest.HttpMethod.GET, action.getUrl(), action);
    }
    catch (Exception ex){
    ex.printStackTrace();
    }
    }


    /**
    * 提交异步Post请求
    * */
    public static void sendPost(ApiAction action) {
    Log.d(LOG_TAG,"@@@@@Post= Url: "+action.getUrl());
    //检查联网状态,允许联网才提交请求。
    try{
    HttpUtils defaultHttpClient = getDefaultHttpClient();
    RequestParams params = new RequestParams();
    params.addHeader("Content-Type", "application/json");
    String objJson = JsonHelper.getGson().toJson(action.getParas());
    // params.addBodyParameter("json", objJson.toString());

    // params.setContentType("application/json");
    params.setBodyEntity(new StringEntity(objJson, "UTF-8"));
    //或者用这个
    // ByteArrayEntity entity = new ByteArrayEntity(objJson.getBytes("UTF-8"));
    // params.setBodyEntity(entity);

    defaultHttpClient.send(HttpRequest.HttpMethod.POST, action.getUrl(), params, action);

    Log.d(LOG_TAG, "@@@@@Post=Json: " + objJson);
    }
    catch (Exception ex){
    //todo:处理网络访问中的异常
    ex.printStackTrace();
    //Log.d(LOG_TAG,ex.toString());
    }
    }


    /**
    * 提交异步Post请求,上传文件(单个)
    * */
    public static void postFileAsync( ApiAction action,String path) {
    Log.d(LOG_TAG,"@@@@@Post=Url: "+action.getUrl());
    File file = new File(path);
    if (file.exists() && file.length() > 0) {
    try {
    HttpUtils defaultHttpClient = getDefaultHttpClient();
    RequestParams params = new RequestParams();


    //判断是否带post参数
    if(action.getParas() !=null){
    params.addHeader("Content-Type", "application/json");
    String objJson = JsonHelper.getGson().toJson(action.getParas());
    params.setBodyEntity(new StringEntity(objJson, "UTF-8"));
    }



    // FileEntity entity = new FileEntity(file, "binary/octet-stream");
    // params.setBodyEntity(entity);

    // 用于非multipart表单的单文件上传
    params.setBodyEntity(new FileUploadEntity(new File(path), "binary/octet-stream"));
    defaultHttpClient.send(HttpRequest.HttpMethod.POST, action.getUrl(), params, action);
    }
    catch (Exception ex){
    //todo:处理网络访问中的异常
    ex.printStackTrace();
    //Log.d(LOG_TAG,ex.toString());
    }
    } else {
    Toast.makeText(action.getContext(), "文件不存在", Toast.LENGTH_LONG).show();
    }
    }

    /**
    * 提交异步Post请求,上传文件(多个)
    * */
    public static void postFileAsync( ApiAction action,String[] paths) {

    ArrayList<File> files = new ArrayList<File>();
    for (int i = 0; i < paths.length; i++) {
    if(paths[i] != null){
    files.add(new File(paths[i]));
    }
    }
    if(files!=null && files.size()>0) {
    try {


    HttpUtils defaultHttpClient = getDefaultHttpClient();
    RequestParams params = new RequestParams();

    //判断是否带post参数
    if(action.getParas() !=null){
    params.addHeader("Content-Type", "application/json");
    String objJson = JsonHelper.getGson().toJson(action.getParas());
    params.setBodyEntity(new StringEntity(objJson, "UTF-8"));
    }


    for (int i = 0; i < files.size(); i++) {
    // 用于多文件上传
    params.addBodyParameter("file" + i, files.get(i), "application/octet-stream");
    }

    defaultHttpClient.send(HttpRequest.HttpMethod.POST, action.getUrl(), params, action);
    } catch (Exception ex){
    //todo:处理网络访问中的异常
    ex.printStackTrace();
    //Log.d(LOG_TAG,ex.toString());
    }

    } else {
    Toast.makeText(action.getContext(), "文件不存在", Toast.LENGTH_LONG).show();
    }
    }

    }
  • 相关阅读:
    红黑树数据结构剖析
    miniui表单验证守则总结
    常用的JS页面跳转代码调用大全
    Jsp页面跳转和js控制页面跳转的几种方法
    处理和引发事件
    HeaderHandler 委托
    序列化SoapFormatter
    Debug.Assert
    C#的Thread类
    再次学习线程概念
  • 原文地址:https://www.cnblogs.com/Android-FJH/p/5481566.html
Copyright © 2020-2023  润新知