In android, sometimes we don't need to use the local SQLite database to provide data. More often, we need to get data from the network. How can Android get data from the server? There are many types.
I. Methods for obtaining data based on Http protocol. Ii. Methods for obtaining data based on the SAOP protocol; 3. Forgot -------
In this article, we will mainly discuss how to use the Http protocol to obtain server-side data. Here we adopt the server-side technology as java, and the framework as Struts2, or we can have Servlet, or you can directly obtain data from the JSP page.
Then, let's start this journey:
First of all: Compile the server method. The MVC framework I use here is Struts2. The purpose is very simple. It is to create a complete commercial project in the future. The technical configuration is android + SSH. Of course, the length is limited. I will use Strtus2 directly here.
Server: Create a WebProject and select Java ee 5.0.
To add Struts2 support to the project,We must import some Struts2 class libraries as follows (some jar packages are not required, but we may need to use the extension later, so we should first get it in ):
1: xwork-core- 2.2.1 . 1.jar
2: struts2-core- 2.2.1 . 1.jar
3: commons-logging- 1.0.4 . Jar
4: freemarker- 2.3.16 . Jar
5: ognl-3.0.jar
6: javassist-3.7.ga.jar
7: commons-ileupload.jar
8: commons-io.jar
9: json-lib-2.1-jdk15.jar to process JSON format data to use
10: struts2-json-plugin- 2.2.1 . 1.jar json plug-in based on struts2
The above jar package needs to be placed in the WebRoot/WEB-INF/lib directory
In the web. xml file, click:
View Code
<?Xml version = "1.0" encoding = "UTF-8"?>
<Web-appVersion= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns: xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<! --Define the core controller of Struts2: FilterDispatcher-->
<Filter>
<! --Define the name of the core Filter-->
<Filter-name>Struts2</Filter-name>
<! --Define the implementation class of Filter-->
<Filter-class>Org. apache. struts2.dispatcher. FilterDispatcher</Filter-class>
</Filter>
<Filter-mapping>
<Filter-name>Struts2</Filter-name>
<Url-pattern>/*</Url-pattern>
</Filter-mapping>
<Welcome-file-list>
<Welcome-file>Index. jsp</Welcome-file>
</Welcome-file-list>
</Web-app>
Then write the struts. xml file and put it in the WebRoot/WEB-INF/lib directory: the following code: