这是我的多部分消息,其中包含用户令牌:
"url":"http://freedoctor.southeastasia.cloudapp.azure.com/reports/add",
"method": "POST",
"headers":
{
"authorization": "JWT token",
},
"mimeType": "multipart/form-data",
data:
{
file: [ multiple files that patient wants to upload],
category: "like denstis"
}
res:
{
message: file upload success
}
这是我的MainActivity
课程:
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import org.json.JSONObject;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ArrayList<String> imagesPathList;
Map<String, File> mFilePartData;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
//Auth header
Map<String, String> mHeaderPart= new HashMap<>();
mHeaderPart.put("mimeType", "multipart/form-data;");
mHeaderPart.put("access_token",
"token");
//File part
mFilePartData= new HashMap<>();
// mFilePartData.put("file", new File(mFilePath));
// mFilePartData.put("file", new File(mFilePath));
//String part
Map<String, String> mStringPart= new HashMap<>();
mStringPart.put("profile_id","1");
mStringPart.put("imageType", "ProfileImage");
CustomMultipartRequest mCustomRequest = new CustomMultipartRequest(Request.Method.POST, getBaseContext(), "http://freedoctor.southeastasia.cloudapp.azure.com/reports/add", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Log.d("JSONObject",jsonObject.toString());
Response.Listener<JSONObject> listener=null;
listener.onResponse(jsonObject);
System.out.println("listnernull");
System.out.println(jsonObject.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("VOLLEYError",volleyError.toString());
Response.ErrorListener listener =null;
listener.onErrorResponse(volleyError);
System.out.println("ERRornull");
System.out.println(volleyError.toString());
}
}, mFilePartData, mStringPart, mHeaderPart); }
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
File file=new File(selectedImageUri.getPath());
Log.d("FILE",file.toString());
selectedImagePath = getPath(selectedImageUri);
mFilePartData.put("file", file);
}
}
}
public String getPath(Uri uri)
{
// just some safety built in
if (uri == null)
{
// TODO perform some logging or show user feedback
return null;
}
String result = null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null)
{
int columnIndex = 0;
try {
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
result = cursor.getString(columnIndex);
} catch (IllegalArgumentException e) {
Log.e("Whilegettingpathforfile", String.valueOf(e));
} finally {
try {
if (!cursor.isClosed()) {
cursor.close();
}
cursor = null;
} catch (Exception e) {
Log.e("While closing cursor", String.valueOf(e));
}
}
}
// this is our fallback here
return uri.getPath();
}
}
这是我的CustomMultipartRequest
:
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.webkit.MimeTypeMap;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
public class CustomMultipartRequest extends Request<JSONObject>
{
private final Response.Listener<JSONObject> mListener;
private final Map<String, File> mFilePartData;
private final Map<String, String> mStringPart;
private final Map<String, String> mHeaderPart;
private MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
private HttpEntity mHttpEntity;
private Context mContext;
public CustomMultipartRequest(int method, Context mContext, `String url, Response.Listener<JSONObject> listener, `Response.ErrorListener errorListener, Map<String, File> mFilePartData, `Map<String, String> mStringPart, Map<String, String> mHeaderPart)
{
super(method, url, errorListener);
mListener = listener;
this.mFilePartData = mFilePartData;
this.mStringPart = mStringPart;
this.mHeaderPart = mHeaderPart;
this.mContext = mContext;
mEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
buildMultipartFileEntity();
buildMultipartTextEntity();
mHttpEntity = mEntityBuilder.build();
}
public static String getMimeType(Context context, String url)
{
Uri uri = Uri.fromFile(new File(url));
String mimeType = null;
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
ContentResolver cr = context.getContentResolver();
mimeType = cr.getType(uri);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
.toString());
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
fileExtension.toLowerCase());
}
return mimeType;
}
private void buildMultipartFileEntity()
{
for (Map.Entry<String, File> entry : mFilePartData.entrySet()) {
try {
String key = entry.getKey();
File file = entry.getValue();
String mimeType = getMimeType(mContext, file.toString());
mEntityBuilder.addBinaryBody(key, file, ContentType.create(mimeType), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void buildMultipartTextEntity()
{
for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && value != null)
mEntityBuilder.addTextBody(key, value);
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
return mHeaderPart;
}
@Override
public String getBodyContentType()
{
return mHttpEntity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
mHttpEntity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response)
{
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response)
{
mListener.onResponse(response);
}
}
我想使用MIME-multipart将多个图像发送到服务器 我正在使用凌空 任何人都可以帮我吗?