Android: AndroidAnnotations: the network interface for uploading files is so simple
Network interfaces are so concise
Use HttpClient to upload files. Refer to blog: Use HttpClient to upload files.
If the project uses AndroidAnnotation, It is very convenient to write the upload interface, such as the previously written POST interface.
First, refer to the previous blog and use AndroidAnnotations for post requests.
The following is a network interface for file upload using AndroidAnnotations.
@Rest(rootUrl = "http://192.168.31.183:8080/SSHMySql/", converters = {GsonHttpMessageConverter.class})public interface FileService extends RestClientErrorHandling{ @Post("/upload.action") @Accept(MediaType.APPLICATION_JSON) ReqResult uploadFile(MultiValueMap
params);}
During the call, the parameter is passed. The "userImage" field is the field of the file received by the server, and desc is another parameter. Of course, you can also add additional parameters.
The following code calls a network interface:
public ReqResult postUploadFile(String localFilePath,String desc) { try { if (!new File(localFilePath).exists()) { return null; } MultiValueMap
params = new LinkedMultiValueMap
(); params.add("userImage", new FileSystemResource(localFilePath)); params.add("desc",desc); return fileService.uploadFile(params); } catch (Exception e) { e.printStackTrace(); } return null; }
Here, we need to note that the value of the "userImage" parameter is new FileSystemResource (localFilePath)
QQ group of Android Development Alliance:272209595
Not for commercial purposes without permission