Just wrote a blog, mentioned in the use of xutils a problem encountered in the Android Xutils framework Httputil GET request cache problem, since the problem has been raised, then I think about the previous use of xutils encountered a few small problems to tidy up.
One, Httputil GET request cache problem
With regard to the GET request method for the HTTP module of the Xtuls framework, there is a caching problem-that is, when a GET request is made, if the same request URL is sent during the cache's specified time, the server will not be asked again, and the result of the last request is returned directly. Previously wrote a blog has been described in detail, you can view the "Android xutils framework Httputil GET request cache problem."
Second, how to send a string directly on the HTTP request
First of all, I would like to say, how to use Xutils Framework HTTP module to send data to the server, here must introduce a API:com.lidroid.xutils.http.RequestParams, I do not post the source of the class, and the method for everyone to see:
As can be seen from the above method, it provides several methods very convenient, not only can directly use the Addquerystringparameter (string name, String value) method, the parameter in the form of key-value into it, (typically used for get requests), as well as Addbodyparameter (string name, String value), with arguments placed in the body of the HTTP, and AddHeader (string name, String value), The header that allows HTTP requests to be added is convenient.
The following people will ask, if I do not upload the parameters in the form of key-value, but want to directly put a string of characters (JSON string, ordinary string, etc.) to the server, then what to do?
There's a way!
1. For a GET request, if you want to upload a string of characters directly, you can directly connect it to the URL.
For example, I want to request the address is: http://www.imooc.com/api/teacher, need to upload the parameter is "{" id ": 1," learner ": 12312}", then I can directly splicing him on the URL, into the "http:// Www.imooc.com/api/teacher? {"id": 1, "learner": 12312} "
The server in accordance with the Convention, and then the corresponding processing is good. Of course, for the above request, there is a special character in the string, and we require that it be URL-encoded: Urlencoder.encode () before it can be uploaded.
2. for post requests, use Requestparams.setbodyentity (httpentity bodyentity)
If we are uploading a string, we can use the subclass instance of Httpentity stringentity: requestparams.setbodyentity (New Stringentity ("")).
Iii. Solutions to Java.net.URISyntaxException
This happens when you use the Xutils Http GET request to make a project a few days ago, when I send a string that contains special characters. Found some online information, said the address is involved in special characters, such as ' | ' & ' and so on. Therefore, you cannot use string instead of URI to access it. You must replace special characters with%0xxx. But this approach is not intuitive. So you can only turn the string into a URL, and then the URL generation Uri method to solve the problem. The code is as follows:
URL url = new URL (strurl); Uri uri = new Uri (Url.getprotocol (), Url.gethost (), Url.getpath (), url.getquery (), NULL); HttpClient Client = new Defaulthttpclient (); HttpGet httpget = new HttpGet (URI);
Understand here, so we look at the xutils source of Uribuilder.java sources, found that its construction method is directly to the incoming URL constructed into a URI:
Public UriBuilder (final String uri) { try { Digesturi (new URI)); } catch (URISyntaxException e) { LOGUTILS.E (E.getmessage (), E); } }
Briefly modify the following source code:
Public UriBuilder (String URI) { try { URL e = new URL (URI); This.digesturi (New URI (E.getprotocol (), E.gethost (), E.getpath (), E.getquery (), (String) null)); } catch (Exception var3) { logutils.e (Var3.getmessage (), VAR3); } }
This is good, recompile the Xutil.jar file, you can rest assured that the use of.
Of course, as I said before, for the special characters of Get requests, we can use Urlencoder.encode () to encode special characters to solve them. However, I tried, with this method did not solve my problem, so I took the way to modify the source code. Interested students can try again.
Postscript:
Recommend a few relatively good articles about the Xutils framework, quite comprehensive:
1. http://www.tuicool.com/articles/nMFb2q Android Open source project xutils httputils Module Analysis
2. Https://github.com/wyouflf/xUtils xutils GitHub Address
3. Introduction to basic use of http://blog.csdn.net/zuiwuyuan/article/category/2217073 xutils framework
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Xutils Framework usage issues and solutions