Just made a post, express a problem, uh, trying to solve ......
The company is now doing an android client to other sites, somewhat similar to Tencent Weibo, the use of official api interface development, but now the demand is to upload photos, update the progress bar, SDK no such function, so it is necessary write your own.
My idea is to use HttpURLConnection to establish a connection, the picture into a stream, and then transmitted to the server, but this does not listen to the progress of the output transport stream, in the end how to do in order to monitor the progress of the stream to transport it - please help gurus help study for three days, not resolved ...... tears ah
Here are the main code:
con = (HttpURLConnection) url.openConnection ();
con.setDoInput (true);
con.setDoOutput (true);
con.setUseCaches (false);
con.setRequestMethod ("POST");
con.setRequestProperty ("Content-Type", contentType);
con.setRequestProperty ("Authorization", "OAuth" + token);
con.connect ();
The above httpHeader, is the format of the official API provides no errors. (If using strict custom form format, I would not tangle schedule problem)
DataOutputStream dos = new DataOutputStream (con.getOutputStream ());
InputStream fStream = new FileInputStream (path);
byte [] buffer = new byte [1024];
int length = -1;
while ((length = fStream.read (buffer))! = -1) {
dos.write (buffer);
}
fStream.close ();
dos.flush ();
dos.close ();
con.getResponseCode ();
The thought that in the above cycle, in batches in the file information read inflow monitor progress can be achieved, but after practice it was found, the system is to transfer the entire file is read out after the inflow, ie after the end of the transmission cycle Data ...... now just do not know how to listen to transmit data at the time of data transmission ......
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Also have the same problem, get progress http filed, the landlord did not solve?
Reply:
The landlord did not solve?
Reply:
Have been resolved long ago project, and remember very clearly, just vaguely remember, you need to set con connection does not use cache (that is an attribute, do not remember specifically what), to ensure the server to accept a write once, rather than write over After the one-time upload
Reply:
Landlord can send some answers? The problem then being tangled
Reply:
Good landlord sharp, HttpURLConnection set this property setUseCaches (false); really helpful
Reply:
I have encountered this problem, how to upload files to obtain upload progress, seeking answers to the landlord, the best detail, thank you
Reply:
Long time no forum, sorry, the answer has been given, 5th Floor ~
Reply:
Useful? The above code is not the landlord has been set yet? How to get the amount of data already uploaded it? ?
No comments:
Post a Comment