1. User Information get--bean entity class form return Data ① server side:
Code:
1 protected voidOnhandler (httpservletrequest request, httpservletresponse response, PrintWriter printwriter) {2map<string, object> map =NewHashmap<string, object>();3 4 Try {5String responses =Onresponse (request, response);6Map.put ("Data", responses);//string, JSON, URL7Map.put ("Error",1);//1 on behalf of server execution success8Map.put ("msg","OK");9}Catch(Exception e) {TenMap.put ("Error","-1");//error is--1 when there is a problem on the service side OneMap.put ("msg","server-side operations database exception occurred"); AMap.put ("Data",""); - } - theJsonobject Jsonobject =(Jsonobject) Json.tojson (map); - Printwriter.write (jsonobject.tojsonstring ()); - } - + protectedString Onresponse (httpservletrequest request, httpservletresponse response) throws Exception { -map<string, string> map =NewHashMap (); +Map.put ("UserName","gan Chuan Spectrum"); AMap.put ("Password","123456"); at returnjson.tojsonstring (map); -}
Json:
{"msg":"OK","data":" {\ "password\": \ "123456\", \ "username\": \ "gan Chuan Spectrum \"} ","error":1}
② Client:
Code:
Public classUserInfo {@JSONField (name="UserName") PrivateString UserName; @JSONField (Name="Password") PrivateString password; PublicUserInfo () {super (); } PublicUserInfo (String userName, string password) {super (); This. UserName =UserName; This. Password =password; }
String URL ="Http://172.16.10.240:8080/noHttpServer/UserInfoServlet"; Request<BaseBean> request =NewBaserequest<basebean> (URL, Basebean.class); Request (0, request, CallBack,false,true);============== Callback ========================Basebean Basebean= Response.Get();if(Basebean.issuccess ()) {UserInfo content= Basebean.parsedata (UserInfo.class); String s="User name:"+ content.getusername () +"; Password:"+Content.getpassword (); Mtvresult.settext (s);} Else{Mtvresult.settext ("Business Processing layer data failure");}
2. Book Information--list collection form return Data ① server side:
Code:
1 protected voidOnhandler (httpservletrequest request, httpservletresponse response, PrintWriter printwriter) {2map<string, object> map =NewHashmap<string, object>();3 4 Try {5String responses =Onresponse (request, response);6Map.put ("Data", responses);//string, JSON, URL7Map.put ("Error",1);//1 on behalf of server execution success8Map.put ("msg","OK");9}Catch(Exception e) {TenMap.put ("Error","-1");//error is--1 when there is a problem on the service side OneMap.put ("msg","server-side operations database exception occurred"); AMap.put ("Data",""); - } - theJsonobject Jsonobject =(Jsonobject) Json.tojson (map); - Printwriter.write (jsonobject.tojsonstring ()); - } - + protectedString Onresponse (httpservletrequest request, httpservletresponse response) throws Exception { -map<string, object> map =NewHashMap (); +list<string> books =NewArrayList (); ABooks.add ("Java"); atBooks.add ("Android"); -Books.add ("IOS"); -Books.add ("C #"); -Books.add (". NET"); -Books.add ("PHP"); -Map.put ("Books", books); in returnjson.tojsonstring (map); -}
Json:
{"msg":"OK","data":" {\ "books\": [\ "Java\", \ "android\", \ "Ios\", \ "c#\", \ ". Net\", \ "Php\"]}"," Error ":1}
② Client:
Code:
Public class booksinfo { "books") private list<string> list; Public BooksInfo () { super (); } Public BooksInfo (list<string> List) { super (); this. List = list; }
String URL ="Http://172.16.10.240:8080/noHttpServer/BookServlet"; Request<BaseBean> request =NewBaserequest<basebean> (URL, Basebean.class); Request (1, request, CallBack,false,true); ========== Callback ==================================Basebean Basebean= Response.Get();if(Basebean.issuccess ()) {BooksInfo content= Basebean.parsedata (booksinfo.class); List<String> list =content.getlist (); String s=""; for(intI=0; I<list.size (); i++) {s+="Book name"+i +":"+ list.Get(i) +"\ n"; } mtvresult.settext (s);} Else{Mtvresult.settext ("Business Processing layer data failure");}
Complete code: Https://github.com/ganchuanpu/StarHttp
Nohttp Package--08 Receive all interface data with an entity class