Android: AndroidAnnotations: the network interface for uploading files is so simple

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.