Xutils Framework Network Request

Source: Internet
Author: User

Github:https://github.com/wyouflf/xutils

About the methods of network requests

Package Com.jike.shanglv.netandjson;import Java.io.file;import Com.lidroid.xutils.httputils;import Com.lidroid.xutils.exception.httpexception;import Com.lidroid.xutils.http.requestparams;import Com.lidroid.xutils.http.responseinfo;import Com.lidroid.xutils.http.responsestream;import Com.lidroid.xutils.http.callback.requestcallback;import Com.lidroid.xutils.http.client.httprequest;import Com.lidroid.xutils.util.logutils;public class Httputil {String result = "";/** * GET request async * @param URL server address * @param UserKey * @param str * @param sign Signature * @return */public string xutilsget (string url, string userkey, String str, str ing sign) {requestparams params = new Requestparams ();p arams.addquerystringparameter ("UserKey", UserKey); Params.addquerystringparameter ("str", str);p arams.addquerystringparameter ("sign", sign); Httputils http = new Httputils (); Http.configcurrenthttpcacheexpiry (1000 * 10); Set timeout time 10shttp.send (Httprequest.httpmethod.get,url, New requestcallback<string> ({@Overridepublic void onloading (long total, long current, Boolean isuploading) {} @Overridepublic void onsuccess ( Responseinfo<string> responseinfo) {result = ResponseInfo.result.toString ();} @Overridepublic void OnStart () {} @Overridepublic void OnFailure (httpexception error, String msg) {}}); return result;  /** * POST request async * @param URL * @param userkey * @param str * @param sign * @return */public string xutilspost (string URL , string userkey, String str, string sign) {Requestparams params = new Requestparams ();p arams.addquerystringparameter (" UserKey ", UserKey);p arams.addquerystringparameter (" str ", str);p arams.addquerystringparameter (" sign ");// The default use of bodyparamsentity,//is similar to urlencodedformentity ("application/x-www-form-urlencoded") when it contains only string parameters. Params.addbodyparameter ("name", "value"), or//Multipartentity ("Multipart/form-data") is used by default after adding the file parameter,//if required "multipart/ Related ", the Multipartentity support settings provided in Xutils are subtype to" related ". Use Params.setbodyentity (httpentity) to set more types of httpentity such as://Multipartentity,bodyparamsentity,fileuploadentity,inputstreamuploadentity,stringentity). For example, send JSON parameter: params.setbodyentity (new Stringentity (Jsonstr,charset)); Httputils http = new Httputils (); Http.configcurrenthttpcacheexpiry (1000 * 10); Set timeout time 10s http.send (HttpRequest.HttpMethod.POST, URL, params,new requestcallback<string> () {@Overridepublic void OnStart () {} @Overridepublic void onloading (long total, long current, Boolean isuploading) {} @Overridepublic void Onsu Ccess (responseinfo<string> responseinfo) {result = ResponseInfo.result.toString ();} @Overridepublic void OnFailure (httpexception error, String msg) {}}); return result;} /** * The POST request with upload file asynchronous * @param URL * @param userkey * @param str * @param sign * @param picstring file address * @return */p Ublic string xutilsfilepost (string url, string userkey, String str, string sign, string picstring) {requestparams par AMS = new Requestparams ();p arams.addquerystringparameter ("UserKey", UserKey);p Arams.addquerystringparameteR ("str", str);p arams.addquerystringparameter ("sign", sign), or//bodyparamsentity,//is used by default when only string arguments are included Similar to Urlencodedformentity ("application/x-www-form-urlencoded"). Params.addbodyparameter ("name", "value"), or//Multipartentity ("Multipart/form-data") is used by default after adding the file parameter,//if required "multipart/ Related ", the Multipartentity support settings provided in Xutils are subtype to" related ". Use Params.setbodyentity (httpentity) to set more types of httpentity (such as://Multipartentity,bodyparamsentity,fileuploadentity, inputstreamuploadentity,stringentity). For example, send JSON parameter: params.setbodyentity (new Stringentity (Jsonstr,charset));p arams.addbodyparameter ("Picture", New File ( picstring)); Com.lidroid.xutils.HttpUtils http = new Com.lidroid.xutils.HttpUtils (); Http.send ( HttpRequest.HttpMethod.POST, URL, params,new requestcallback<string> () {@Overridepublic void OnStart () {}@ overridepublic void onloading (long total, long current, Boolean isuploading) {} @Overridepublic void onsuccess ( Responseinfo<string> responseinfo) {result = ResponseInfo.result.toString ();} @Overridepublic void OnFailure (httpexception error, String msg) {}}); return result;} -------------------The code above is an asynchronous request, the following code is the-------------------------of the synchronous request//
/** * Get sync request must execute in async block * @param URL * @param userkey * @param str * @param sign * @return */private String Xutilsgetsync (string URL, string userkey, String str, string sign) {Requestparams params = new Requestparams ();p arams.addquerystringparameter ("UserKey", UserKey); Params.addquerystringparameter ("str", str);p arams.addquerystringparameter ("sign", sign); Httputils http = new Httputils (); Http.configcurrenthttpcacheexpiry (1000 * 10); Set timeout time try {responsestream responsestream = Http.sendsync (Httprequest.httpmethod.get,url, params);//int StatusCode = Responsestream.getstatuscode ();//header[] headers = Responsestream.getbaseresponse (). Getallheaders (); return Responsestream.readstring ();} catch (Exception e) {logutils.e (E.getmessage (), e);} return null;} /** * Post synchronization request must be executed in the async block * @param URL * @param userkey * @param str * @param sign * @return */private String Xutilspostsy NC (string URL, string userkey, String str, string sign) {Requestparams params = new Requestparams ();p ARams.addquerystringparameter ("UserKey", UserKey);p arams.addquerystringparameter ("str", str); Params.addquerystringparameter ("sign", sign); Httputils http = new Httputils (); Http.configcurrenthttpcacheexpiry (1000 * 10); Set timeout time try {responsestream responsestream = Http.sendsync (HttpRequest.HttpMethod.POST, url, params);//int StatusCode = Responsestream.getstatuscode ();//header[] headers = Responsestream.getbaseresponse (). Getallheaders (); return Responsestream.readstring ();} catch (Exception e) {logutils.e (E.getmessage (), e);} return null;}}

Xutils Framework Network Request

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.