Nohttp Package--08 Receive all interface data with an entity class

Source: Internet
Author: User
Tags tojson

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

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.