I looked at various sites and tried the source code, but I couldn't find anything that could upload images to the server using Volley.
public class MultipartRequest2 extensions Request<String>{
public static final String KEY_PICTURE="mypicture";
public static final String KEY_PICTURE_NAME="filename";
public static final String KEY_ROUTE_ID = "route_id";
private HttpEntity mHttpEntity;
private String mRouteId;
private Response.Listener;
public MultipartRequest2(String url, String filePath, String routeId,
Response.Listener<String>listener,
Response.ErrorListener errorListener) {
super(Method.POST, url, errorListener);
mRouteId = routeId;
mListener=listener;
mHttpEntity = buildMultipartEntity (filePath);
}
public MultipartRequest2(String url, File file, String routeId,
Response.Listener<String>listener,
Response.ErrorListener errorListener) {
super(Method.POST, url, errorListener);
mRouteId = routeId;
mListener=listener;
mHttpEntity=buildMultipartEntity(file);
}
private HttpEntity buildMultipartEntity (String filePath) {
File file = new File(filePath);
return buildMultipartEntity (file);
}
private HttpEntity buildMultipartEntity (File file) {
MultipartEntityBuilder = MultipartEntityBuilder.create();
String fileName = file.getName();
FileBody fileBody = new FileBody (file);
builder.addPart(KEY_PICTURE, fileBody);
builder.addTextBody(KEY_PICTURE_NAME, fileName);
builder.addTextBody(KEY_ROUTE_ID, mRouteId);
return builder.build();
}
@ Override
public String getBodyContentType(){
return mHttpEntity.getContentType().getValue();
}
@ Override
public byte[]getBody()throws AuthFailureError{
ByteArrayOutputStream boss = new ByteArrayOutputStream();
try{
mHttpEntity.writeTo(bos);
} catch(IOExceptione){
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return boss.toByteArray();
}
@ Override
protectedResponse<String>parseNetworkResponse(NetworkResponse){
return Response.success("Uploaded", getCacheEntry());
}
@ Override
protected void deliveryResponse(String response) {
mListener.onResponse(response);
}
}
I would like to upload an image to the server by POST with this source code I found online.
What is routeId?
In the sample,
HashMap<String, String>params=newHashMap<>();
String url="http://someone.jp/upload.php";
String image_path="/storage/sdcard/DCIM/Camera/IMG_20161026_182039.jpg";
params.put ("user", "username");
MultipartRequest2 multipartRequest=
new MultipartRequest2(url, image_path, params, newResponse.Listener<String>(){
@ Override
public void onResponse (String response) {
Log.e("TAG", "Success Response:" + response.toString());
}
}, newResponse.ErrorListener(){
@ Override
public void onErrorResponse (VolleyError) {
if(error.networkResponse!=null){
Log.e("TAG", "Error Response code:"+
error.networkResponse.statusCode);
try{
dcimPath.setText("Gallery Path:"+"user");
}finally {
}
}
if(error instance of NetworkError) {
} else if (error instance of ServerError) {
} else if (error instance of AuthFailureError) {
} else if (error instance of ParseError) {
} else if (error instance of NoConnectionError) {
} else if (error instance of timeoutError) {
}
}
});
mQueue.add (multipartRequest);
The error occurred and the object could not be generated.
Please tell me who it is.
android java android-volley
I solved myself.
public static final String KEY_PICTURE="mypicture";
public static final String KEY_PICTURE_NAME="filename";
public static final String KEY_ROUTE_ID = "route_id";
More
files can be retrieved at $_POST ['mypicture'] ['tmpname']
The string meant $_POST['route_id'] could be retrieved.
© 2024 OneMinuteCode. All rights reserved.