Volley 网络框架实现文件上传

来源:互联网 发布:颜值高的读书笔记软件 编辑:程序博客网 时间:2024/05/04 03:45

1.首先自己写一个MultipartRequest类,继承自StringRequest。

import com.android.internal.http.multipart.Part;import com.android.volley.AuthFailureError;import com.android.volley.Response;import com.android.volley.VolleyLog;import com.android.volley.toolbox.StringRequest;import java.io.ByteArrayOutputStream;import java.io.IOException;public class MultipartRequest extends StringRequest {    private Part[] parts;    public MultipartRequest(String url, Part[] parts, Response.Listener<String> listener, Response.ErrorListener errorListener) {        super(Method.POST, url, listener, errorListener);        this.parts = parts;    }    @Override    public String getBodyContentType() {        return "multipart/form-data; boundary=" + Part.getBoundary();    }    @Override    public byte[] getBody() throws AuthFailureError {        ByteArrayOutputStream baos = new ByteArrayOutputStream();        try {            Part.sendParts(baos, parts);        } catch (IOException e) {            VolleyLog.e(e, "error when sending parts to output!");        }        return baos.toByteArray();    }}
2.使用方法

//构造参数列表List<Part> partList = new ArrayList<Part>();partList.add(new StringPart("username", "hellfire",“UTF-8”));partList.add(new StringPart("email", "ouyangjun@aliyun.com",“UTF-8”));try {    partList.add(new FilePart("photo", new File("/mnt/sdcard/Test/hellfire.jpg")));} catch (FileNotFoundException e) {    e.printStackTrace();}//获取队列RequestQueue requestQueue = Volley.newRequestQueue(this);String url = "http://test/profileUpdate.do";//生成请求MultipartRequest profileUpdateRequest = new MultipartRequest(url, partList.toArray(new Part[partList.size()]), new Response.Listener<String>() {    @Override    public void onResponse(String response) {        //处理成功返回信息        String info = response.substring(0, 20);        Toast.makeText(getApplication(), info, Toast.LENGTH_SHORT).show();    }}, new Response.ErrorListener() {    @Override    public void onErrorResponse(VolleyError error) {        //处理失败错误信息        Log.e("MultipartRequest", error.getMessage(), error);        Toast.makeText(getApplication(), error.getMessage(), Toast.LENGTH_SHORT).show();    }});//将请求加入队列requestQueue.add(profileUpdateRequest);



原创粉丝点击