A few months ago wrote a blog "xUtils3.0 Framework Learning Notes", there are records through the xutils implementation of File upload method, the code is as follows:
private void Uploadonclick (View v) {
String upurl = "/mnt/sdcard/pic/test.jpg";//Specify the file to upload
final progressdialog Dia = new ProgressDialog (this);
Dia.setmessage ("Loading ...");
Dia.show ();
Requestparams params = new Requestparams (upurl);
Params.addbodyparameter ("File", new file (Upurl));
X.http (). Post (params, new callback.commoncallback<string> () {
@Override public
void Onsuccess (String Result) {
//loading a successful callback, returns the obtained data
log.i (TAG, "onsuccess:" + result);
}
@Override public
void onfinished () {
Dia.dismiss ();//load Complete
} @Override public
void Oncancelled (cancelledexception CeX) {
}
@Override public
void OnError (Throwable ex, Boolean Isoncallback) {
}
});
I am in the project through the above methods to upload pictures, found a little problem, perhaps I use this method is not in place.
When I am in tune with the server, my colleague always tells me that I do not receive the file stream data, and the result is definitely an upload failure. Later continue to look at code follow up and find through
' Params.addbodyparameter ("file", new file (Upurl)); '
This way the file data is passed, and the default request data type is not the Multipart/form-data type data required by the file type.
By looking at the corresponding source code, we use Xutils to implement the network request, through
' requestparams params = new Requestparams (constants.add_zone_plane); ' create request and then pass
' Params.setrequestbody (body); ' pass request parameters.
In fact, the Setrequestbody (Requestbody requestbody) method is the method that invokes the Requestparams's parent class Baseparams:
public void Setrequestbody (Requestbody requestbody) {
this.requestbody = requestbody;
Consider is the parameter data type problem, we start from passing parameter requestbody, found in Xutils, the default is:
Filebody,inputstreambody,multipartbody and Stringbody and several other types of request body, of course these are requestbody subclasses.
This method is found in the Multipartbody type:
private void Generatecontenttype () {
String boundarypostfix = double.tohexstring (Math.random () * 0xFFFF);
Boundarypostfixbytes = Boundarypostfix.getbytes ();
ContentType = "Multipart/form-data; boundary= "+ new String (boundary_prefix_bytes) + Boundarypostfix;
}
See the return type is multipart/form-data ..., exactly what I want, so request parameters from Multipartbody start thinking about uploading files.
The Multipartbody class is constructed in the following ways:
The parameters are the data to be passed in, and the data encoding type Public
multipartbody (list<keyvalue> multipartparams, String charset) {
if (! Textutils.isempty (CharSet)) {
this.charset = charset;
}
This.multipartparams = Multipartparams;
Generatecontenttype ();
...
}
Starting with the parameters required by the construction method, we implement the data required by the Multipartbody step by step.
Create list<keyvalue> object
list<keyvalue> List = new arraylist<> ();
Add data to the list, Filepah is the uploaded file path, such as the SD card image
List.add (new keyvalue ("File", new file (Filepah));//file stream data
//other parameters, Depending on the project, for example, the parameters to be passed in my project are JSON-formatted
list.add (new KeyValue ("Parameters", json.tostring ());
Create Multipartbody
multipartbody BODY = new Multipartbody (list, "UTF-8");
Add request parameter
params.setrequestbody (body);
This will enable file upload, and finally post the entire request method of the relevant code:
JSON format parameters to pass to the server jsonobject JSON = new Jsonobject ();
try {json.put ("Devid", id);
Json.put ("Devname", devname);
Json.put ("KeyWord", KeyWord);
catch (Jsonexception e) {e.printstacktrace ();
//Build the Requestparams object, the server address URL of the incoming request requestparams params = new Requestparams (constants.upload_file);
Params.setasjsoncontent (TRUE);
list<keyvalue> list = new arraylist<> ();
List.add (New KeyValue ("File", new file (Filepah));
List.add (New KeyValue ("Parameters", json.tostring ());
Multipartbody BODY = new Multipartbody (list, "UTF-8");
Params.setrequestbody (body); X.http (). Post (params, new org.xutils.common.callback.commoncallback<string> () {@Override public void on
Success (String result) {LOGUTIL.E ("request:" + results); @Override public void onfinished () {//upload complete} @Override public void oncancelled (
Cancelledexception CeX) {//Cancel upload}
@Override public void OnError (Throwable ex, Boolean isoncallback) {//upload failed LOGUTIL.E ("Request failed:" +
Ex.tostring ());
}
});
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.