In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Http to achieve image upload and form submission in Android. The content of the article is of high quality, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.
The server is mainly Web, the client generally uploads files to the web server through http, the initial idea is very simple, directly convert the image into a byte stream, write it to http outstream, and then send it out.
But when this method fails, the server determines whether the file is received based on the field in the file name form. My simple method above makes it fail every time the server reports that it has not received the image file. From this, it is inferred that there is a problem with form transmission. Android has many methods for form transmission due to historical reasons. The current official recommendation is HttpURLConnection, but there is no formalized form encapsulation method for building forms using HttpURLConnection. For example, for C#form submission, it can be done in a few simple words:
WWWForm form = new WWWForm(); form.AddField("frameCount", Time.frameCount.ToString()); form.AddBinaryData("fileUpload", bytes); // Upload to a cgi script WWW w = new WWW("http://localhost/cgi-bin/env.cgi? post", form);
Java's HttpURLConnection does not have such a simple encapsulation form, requiring a complete request body simulation, which is relatively inconvenient to use, but it can have a clearer understanding of the essence of single commit.
web-side demo
In the case that the Android upload always fails, the background development buddy helps to implement the web-side request demo, which can process the request normally. The page is as follows:
After selecting the file, press F12 of the browser, and the developer tool interface will appear. In the Network column, you can see the specific request and response. Analyze its request header and request body to construct the same parameters in Android, and you can realize the normal upload of files. The following is based on the web-side request demo to simulate the implementation of Android post submission method.
Http Request Header Analysis
Let's start with the request header:
Accept:*/*Accept-Encoding:gzip, deflateAccept-Language:zh-CN,zh;q=0.8,en;q=0.6Content-Length:38275Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryCjkbdjmUGD4QJISLHost:118.69.25.90Origin:http://118.69.25.90Proxy-Connection:keep-aliveReferer:http://118.69.25.90/User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36Request Payload
According to the request header, to build Android HttpURLConnection related parameters:
URL url = new URL("http://118.69.25.90/uploadpicture.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection. setDoInput(true);connection. setDoOutput(true);connection.setUseCaches(true);//Start post method connection.setRequestMethod("POST");//Set request header content connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("Content-Type", "multipart/form-data"; boundary=---WebKitFormBoundaryCjkbdjmUGD4QJISL");Http request body analysis
The following is an analysis of the message body (Request Payload), which reads as follows:
------boundary=----WebKitFormBoundaryCjkbdjmUGD4QJISLContent-Disposition: form-data; name="file"; filename="ouba.jpg"Content-Type: image/jpegÿØÿà 2! ! 22222222222222222222222222222222222222222222222222ÿÂyÛ"{qX½? WóþÔ¾ÅæÑÏRiÙ' ˺h Í'Ÿ>¡&@˜AÈ"L"æÒ²Ä'` z·« ©Ä[x ¯ eu"k1ÑÏ-³¶¬'næZYT4H¶‹ ´tp؃·Rô)ÁÕ1åêå2«ee-fŒ]¬¢kd«'ú~ï'õøÔ™$ Ò-°¢$IË"->ÊNw1S…………………………………………Ü'-È w1…ª(,RÅ·IȪ·'Z~Yô ë7U
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.