private void doInit(final Map<String, String> params, final File uploadFile) { String initUrl = "http://192.168.1.1:8080/cld/init"; final String boundary = "-----------------" + UUID.randomUUID().toString(); JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, initUrl, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { //TO-DO } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { //TO-DO } }) { @Override public String getBodyContentType() { return "multipart/form-data; boundary=" + boundary; } @Override public byte[] getBody() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); if(params != null){ Set<String> keys = params.keySet(); for(Iterator<String> i = keys.iterator(); i.hasNext();) { String key = (String)i.next(); pairs.add(new BasicNameValuePair(key, params.get(key))); } } int size = pairs.size(); StringBuffer sb= new StringBuffer() ; for(int i=0; i<size; i++) { sb.append(TWO_HYPHENS).append(boundary).append(LINE_END); sb.append("Content-Disposition: form-data; name="").append(pairs.get(i).getName()).append(""").append(LINE_END); sb.append(LINE_END) ; sb.append(pairs.get(i).getValue()).append(LINE_END) ; } sb.append(TWO_HYPHENS).append(boundary).append(TWO_HYPHENS).append(LINE_END); try { bos.write(sb.toString().getBytes("utf-8")); } catch (IOException e) { e.printStackTrace(); } return bos.toByteArray(); } }; mQueue.add(request); }
Volley实现(multipart/form-data)Post请求
时间:2024-3-2 18:32 作者:韩俊 分类: Android
标签: android