This article for you to share the Android Retrofit 2.0 frame upload picture solution, specific content as follows
1. Upload of single picture
/**
* Upload a picture
* @param description
* @param imgs
* @return * *
@Multipart
@POST ("/upload")
call<string> uploadimage (@Part ("FileName") String description,
@Part ("file\"; filename=\ "Image.png \ "") Requestbody IMGs);
2. Upload multiple photos
/**
* Upload three pictures
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return c19/>*/
@Multipart
@POST ("/upload")
call<string> uploadimage (@Part ("FileName") String Description,
@Part ("file\"; filename=\ "image.png\") requestbody IMGs,
@Part ("file\"; filename=\ " Image.png\ "") requestbody imgs1,
@Part ("file\"; filename=\ "image.png\") requestbody IMGS3);
Note: the current is to provide 3, to upload the current I found that the method is to want to send one more, to add one more parameter
@Part ("file\"; filename=\ "image.png\") Requestbody IMGs, and so on.
We see that the writing is very leak, but for the limited capacity, can only think of this. After the variable parameters in Java are resolved, only one can be passed. Not more than one.
@Multipart
@POST ("/upload")
call<string> uploadimage (@Part ("FileName") String description,
@ Part ("file\"; filename=\ "image.png\") requestbody; imgs);
Call:
call<string> call = Apimanager.uploadimage (m[0],requestbody1,requestbody2,null);
It looks high-end, unfortunately, only one.
3. Finally, the realization of the HU process
3.1 Creating the Fileuploadservice interface
Public interface Fileuploadservice {
/**
* Upload a picture
* @param description
* @param imgs
* @return
* *
@Multipart
@POST ("/upload")
call<string> uploadimage (@Part ("FileName") String description,
@Part ("file\"; filename=\ "image.png\") Requestbody IMGs);
/**
* Upload three pictures
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return c18/>*/
@Multipart
@POST ("/upload")
call<string> uploadimage (@Part ("FileName") String Description,
@Part ("file\"; filename=\ "image.png\") requestbody IMGs,
@Part ("file\"; filename=\ " Image.png\ "") requestbody imgs1,
@Part ("file\"; filename=\ "image.png\") requestbody imgs3);
3.2 Creating retrofit Objects
Private static final Retrofit Sretrofit = new retrofit. Builder ()
. BaseURL (ENDPOINT)
. Addconverterfactory (Gsonconverterfactory.create ())
// . Addcalladapterfactory (Rxjavacalladapterfactory.create ())///Use Rxjava as callback adapter
. Build ();
private static final Fileuploadservice Apimanager = sretrofit.create (Fileuploadservice.class);
3.3 Calling the Upload method
public static void upload (string path) {string descriptionstring = ' Hello, this is description speaking ';
String[] m = new string[2];
M[0]= "Share.png";
M[1]= "Screenshot_20160128-140709.png";
File[] ssssss= new file[2];
File File1 = new file ("/storage/emulated/0/sc/share.png");
File File = new file ("/storage/emulated/0/pictures/screenshots/screenshot_20160128-140709.png");
Ssssss[0]=file;
Ssssss[0]=file1;
Requestbody requestbody[] = new REQUESTBODY[3];
Requestbody requestBody1 = requestbody.create (Mediatype.parse ("Multipart/form-data"), file);
Requestbody requestBody2 = requestbody.create (Mediatype.parse ("Multipart/form-data"), file1);
requestbody[0]=requestbody1;
Requestbody[1]=requestbody2;
Call<string> call = Apimanager.uploadimage (m[0],requestbody1,requestbody2,null); Call.enqueue (New callback<string> () {@Override public void Onresponse (response<string> Response, retrofit
Retrofit) {LOG.V ("Upload", Response.message ()); LOG.V ("Upload", "success");
@Override public void OnFailure (Throwable t) {log.e ("Upload", t.tostring ());
}
});
}
4. Server Segment Code:
the server is using struts to receive:
@Controller public
class GetToken extends Actionsupport {
/**
* */
private static final long Serialversionuid = 1L;
Private file[] File;
Private string[] FileName;
Public file[] GetFile () {return
File;
}
public void Setfile (file[] file) {
this.file = File;
}
Public string[] GetFileName () {return
fileName;
}
public void Setfilename (string[] filename) {
this.filename = filename;
}
@Action ("/upload") public
Void login () {
System.out.println ("------" +arrays.tostring (file));
SYSTEM.OUT.PRINTLN ("------" +arrays.tostring (FileName));
}
The above is the entire content of this article, I hope to help you learn.