The previous two articles explained, the interface service construction as well as the interface development, if you have not already understood, may read the article first, the Java Web Development (one) environment constructs and the Java Web Development (two) interface development. This article combines the backend interface service with the client. The client obtains the data from the interface and then displays it. (the client uses Android).
One. Interface service creation. You can refer to the previous article, here is not long-winded.
Two. Implement Android project.
1. Create Android Project. I believe everyone will be. (I don't have to say much)
2. Implement JSON data parsing. As mentioned in the previous interface development article, there are 2 kinds of JSON data returned, one is an object, the other is an array. Then we need to implement the parsing object in both formats.
First, you need to create a base class with three fields inside, code, MSG, and time. Specifically implemented as follows:
public class Response implements Serializable {
/**
*
*
/private static final long serialversionuid = 1L;
private String code;
private String msg;
Private Long time = new Date (). GetTime ();
Public String GetCode () {return
code;
}
public void Setcode (String code) {
this.code = code;
}
/**
* @return The time
/public Long getTime ()
}
/** * @param time * "Time" to
set */public
void SetTime (Long time) {
this.time = time;
} public
String getmsg () {return
msg;
}
public void Setmsg (String msg) {
this.msg = msg;
}
public void SetContent (string code, String msg) {
this.code = code;
this.msg = msg;
}
}
This class is the base class for all JSON parsing classes, encapsulating some of the information returned by the server.
2.1 Objects.
public class Entityresponse<t> extends Response {
private static final long Serialversionuid = 3750508105973880 680L;
Private T object;
Public T GetObject () {return
object;
}
public void SetObject (T object) {
This.object = object;
}
}
This class inherits from response and is used to return the case where the JSON data is a separate object.
2.2 Arrays.
public class Listresponse<t> extends Response {
private static final long Serialversionuid = 2857885317968129959L;
private list<t> items;
Public list<t> GetItems () {return
items;
}
public void Setitems (list<t> items) {
this.items = items;
}
}
This class inherits from response, which returns the case where the JSON data is an array.
2.3 JSON parsing. Parsing JSON uses a Gson.
private static <T> T Parsebygson (String json, typetoken<t> typetoken) {
Gson Gson = new Gson ();
T result = Gson.fromjson (JSON, Typetoken.gettype ());
String code = ((com.studentmaneagemobile.mobile.Response) result)
. GetCode ();
String msg = ((com.studentmaneagemobile.mobile.Response) result)
. getmsg ();
Need to determine whether the return code is OK if
(code!= NULL && code.equalsignorecase ("OK")) {returns result
;
}
return null;
}
Parse JSON, and when the code is ' OK ' it is considered successful to return the JSON data before parsing the JSON data. Returns the JSON string and resolution type, for example
typetoken<listresponse<students>> token = new typetoken<listresponse<students>> () {
};
2.4 Code detailed code to download.
2.5 run.
First, start the interface service, after the successful start, enter the interface address in the browser, for example, the following screenshot indicates that the interface service started successfully,
Next, run the client code and click the ' Inq ' button, as shown below,
Three. Summary.
If you've ever done Android or iOS or other client-side development, you'll always need a backend service to provide the interface. To achieve such a function, simple to say can be divided into 2 parts:
1. Interface Service development. For more information, see Java Web Development (i) environment building and Java Web Development (ii) interface development.
2. Client development. Client needs, UI design, network, picture, database or other framework, and so on.
PS: These two parts, in the early stage of design, need a good architect well design. Otherwise, in the late development, there will be a lot of problems.
It seems very simple to sum up, but only the developed people know the taste of it. "Good good study day."
PS: Interface server code, interface services demo download address.
Client code, client demo download address.
JSON formatting tool
Recommended article: SPRINGMVC Development interface
posting often have a small partner message, said that many of the problems of the project, the recent introduction of several pieces of the pit of the article, intends to this several articles in detail to solve the project code and business processes, Java Web Development (iv) PIT 1 and Java Web Development (v) Interface Development Pit 2 . You are welcome to view.