Android using HTTP service

Android using HTTP service

In Android, in addition to using java.net package under the API to access HTTP services, we also can change a way to complete the work. Android SDK comes with Apache's HttpClient API. Apache HttpClient is a complete HTTP client, which provides full support for the HTTP protocol, you can use the HTTP GET and POST access. Here we combine instance, tell us about the use of the HttpClient.
Http we create a project, the project structure is shown:



In this project, we do not need any of Activity, all operations are done in the unit test class HttpTest.java in.
Because the use of the unit test, so here to tell us about how to configure the Android unit testing. All configuration information is done in AndroidManifest.xml:



Then, our unit test class needs to inherit android.test.AndroidTestCase class, which itself is inherited junit.framework.TestCase, and provides getContext () method is used to get Android context, this design is very useful because many Android Context API are required to complete.
Now let's look at our test case, HttpTest.java code is as follows:




Because this file contains three test cases, one by one so I will explain.
First, note that we use when positioning server address to the IP, because there can not be localhost, the server is running on the windows, and this unit test run in the Android platform, it means that if you use localhost to access the interior Android services may not be accessed, it must use IP to location services.
We first analyze testGet test cases. We use a HttpGet, request parameters directly attached to the back URL, then perform a GET request by the HttpClient, if the response is successful, made within the response stream as input and convert it to a string, and finally determine whether the GET_SUCCESS.
testGet the corresponding server-side testing Servlet code is as follows:



Then say testPost test cases. We use a HttpPost, URL back and did not come with the parameter information, parameter information is packaged into a form set by the NameValuePair type composition, and then process the call after UrlEncodedFormEntity HttpPost of setEntity method parameter settings, and finally executed by HttpClient.
testPost test corresponding server-side code is as follows:


The above two are the basic GET requests and POST requests, arguments are text data types, to meet the needs of the ordinary, but in some situations, for example, when we use to upload files, you can not use the basic GET and POST requests request, and we want to use a multi-part POST request. Here's how to use multi-part POST operations upload a file to the server.
Since HttpClient version shipped with Android does not support multi-part POST request, so we need to use a HttpMime open source project, this component is to deal specifically with the MIME type of related operations. Because HttpMime HttpComponents is included in the project, so we need to go to the official website to download apache HttpComponents, then put them into the project to go HttpMime.jar package, as shown:



Then, we observe testUpload test, InputStreamBody processing document flow parameters we use HttpMime provided with StringBody deal with ordinary text parameters, and finally all the type parameters are added to a MultipartEntity instance, and this is set to the POST request multipartEntity parameter entities, and then perform the POST request. Servlet server code is as follows:




The service uses apache open source project FileUpload processing, so we need to jar package commons-fileupload and commons-io these two projects, the development of the service side are not familiar with friends can go online to look at the relevant information.
After the above described three different situations, we need to consider a problem in practice, we can not always create HttpClient, but should only create an HttpClient for the entire application, and use it for all HTTP traffic. In addition, it should be noted that multi-threading problems that may occur when passing through a HttpClient also issued multiple requests. For these two issues, we need to improve the look of our project:
1. Expand the system default Application, and applied in the project.
2. Use ThreadSafeClientManager HttpClient library provides to create and manage HttpClient.
Project structure improved as shown:


MyApplication which extends the system's Application, the following code:




We rewrite the onCreate () method, when the system starts to create an HttpClient; rewrite the onLowMemory () and onTerminate () method to close the connection at the end of insufficient memory, and application, free up resources. It should be noted that, when instantiated DefaultHttpClient, passing an instance from one ClientConnectionManager ThreadSafeClientConnManager created, responsible for managing the HttpClient HTTP connection.
Then, I want to let this enhanced version of "Application" effect, you need to do the following configuration in AndroidManifest.xml:


If we are not configured, the system will default to android.app.Application, we added the configuration, the system will use our com.scott.http.MyApplication, then you can call in the context of the getApplication () to get the MyApplication instances.
With the above configuration, we can apply in the event of, HttpActivity.java code is as follows:





Click the "execute" button, the execution results are as follows:



Share this

Related Posts

Previous
Next Post »