Android Xutils framework usage Problems and Solutions

Source: Internet
Author: User

Android Xutils framework usage Problems and Solutions

I have just written a blog post and mentioned a problem that I encountered when using XUtils. The Android Xutils framework HttpUtil Get request cache problem. Now that I have raised this problem, I have thought about it, let's sort out several small issues that we encountered when using Xutils.

 

I. HttpUtil Get request cache Problems

The get Request Method of the Http module of the Xtuls framework has a cache problem-that is, when a get request is made, if the same request url is sent within the specified period of time, the server will not send the request again, but will directly return the result of the previous request. I have previously written a blog article that has already introduced it in detail. You can refer to the Android Xutils framework HttpUtil Get request cache question.

 

2. How to send a string directly in an HTTP request

First, I would like to explain how to use the HTTP module of the Xutils framework to send data to the server. Here we must introduce an API: com. lidroid. xutils. http. requestParams: I will not post the source code of this class, and I will show you the method:

It can be seen from the above method that it provides several very convenient methods, not only can you directly use addQueryStringParameter (String name, String value) method, put the parameter in the form of key-value, (usually used for get requests), addBodyParameter (String name, String value), where parameters are placed in the Http body, And addHeader (String name, String value, adding HTTP request headers is convenient.

Someone will ask, if I don't upload parameters in the form of key-value, but want to directly upload a string of characters (JSON string, common string, etc.) to the server, what should we do?

There is a solution!

1. For Get requests, if you want to directly upload a string of characters, you can directly connect it to the url.

For example, if I want to request the address is: http://www.imooc.com/api/teacher, then the upload is always id:1, learner: 12312} ", then I can directly splice it on the url, into a http://www.imooc.com/api/teacher? {Id: 1, learner: 12312}

The server can proceed with the corresponding processing according to the Conventions. Of course, for the above request, there are special characters in the string, we need to perform URL encoding: URLEncoder. encode () before uploading.

 

2.For Post requests, use requestParams. setBodyEntity (HttpEntity bodyEntity)

If we upload a string, use the StringEntity subclass instance of HttpEntity to: requestParams. setBodyEntity (new StringEntity ()).

 

Iii. java.net. URISyntaxException Solution

When Xutils Http get request is used for a project in the previous day, this happens when a string I sent contains special characters. I found some online materials and said the address contains special characters, such as '|. Therefore, you cannot directly use String instead of URI for access. Special characters must be replaced by % 0xXX. However, this method is not intuitive. Therefore, the String can only be converted into a URL first, and then the URI can be generated through the URL 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);

After reading this, let's look at the URIBuilder. java source file of the Xutils source code and find that its construction method is to directly construct the imported url into a uri:

 

 

    public URIBuilder(final String uri) {        try {            digestURI(new URI(uri));        } catch (URISyntaxException e) {            LogUtils.e(e.getMessage(), e);        }    }
Briefly modify the 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);        }    }
You can use the xutil. jar file again.

 

 

Of course, as mentioned earlier, we can use URLEncoder. encode () to encode special characters in get requests. However, I tried to solve my problem by using this method, so I adopted the method of modifying the source code. If you are interested, try again.

 

 

Related Article

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.