Nohttp Package--02

Source: Internet
Author: User

Bean entity class Request:

1.bean

1 import java.io.Serializable;2 import Com.alibaba.fastjson.annotation.JSONField;3 4  Public classUserInfo implements Serializable {5     Private StaticFinalLongSerialversionuid =145641645L;6 7@JSONField (name ="Data")8     PrivateString data;9@JSONField (name ="Error")Ten     Private interror; One@JSONField (name ="msg") A     PrivateString msg; -  -      PublicUserInfo () { the super (); -     } -  -      PublicUserInfo (String data,interror, String msg) { + super (); -          This. data =data; +          This. Error =error; A          This. msg =msg; at     } -      PublicString GetData () { -         returndata; -     } -      Public voidsetData (String data) { -          This. data =data; in     } -      Public intGetError () { to         returnerror; +     } -      Public voidSetError (interror) { the          This. Error =error; *     } $ Panax Notoginseng      PublicString getmsg () { -         returnmsg; the     } +      Public voidsetmsg (String msg) { A          This. msg =msg; the     } +  -      PublicBoolean issucceed () { $         return 1==error; $     } -  -}

2. Request for Bean

1 import Com.alibaba.fastjson.JSON;2 import com.yolanda.nohttp.Headers;3 import com.yolanda.nohttp.JsonObjectRequest;4 import Com.yolanda.nohttp.RequestMethod;5 import com.yolanda.nohttp.RestRequest;6 import com.yolanda.nohttp.StringRequest;7 import Com.yolanda.nohttptest.bean.UserInfo;8 9  Public classUserinforequest extends Restrequest<userinfo> {Ten  One      Publicuserinforequest (String URL, Requestmethod requestmethod) { A super (URL, requestmethod); -     } -      Publicuserinforequest (String url) { the super (URL); -     } - @Override -      PublicString getaccept () { +         returnjsonobjectrequest.accept; -     } + @Override A      PublicUserInfo parseresponse (String URL, Headers responseheaders,byte[] responsebody) { atUserInfo info =NULL; -Stringstring=stringrequest.parseresponsestring (URL, responseheaders, responsebody); -         Try { -info = Json.parseobject (string, UserInfo.class); -}Catch(Exception e) { -info =NewUserInfo (); in         } -         returninfo; to     } +}

3. Specifically used in the activity:

1  Public classObjectactivity extends Activity implements View.onclicklistener {2 3     PrivateFinalintNohttp_login =0x01;4     PrivateFinalintNohttp_logout =0x02;5 6     PrivateTextView Tvresult;7 8 @Override9     protected voidonCreate (Bundle savedinstancestate) {Ten super.oncreate (savedinstancestate); One Setcontentview (r.layout.activity_object); AFindviewbyid (R.id.btn_login). Setonclicklistener ( This); -Findviewbyid (r.id.btn_logout). Setonclicklistener ( This); -Tvresult =(TextView) Findviewbyid (r.id.tv_result); the     } -  - @Override -      Public voidOnClick (View v) { +         if(V.getid () = =R.id.btn_login) { -Userinforequest request =Newuserinforequest (Constants.login, requestmethod.get); +Request.add ("UserName","Yolanda"); ARequest.add ("userpwd","123"); atCallserver.getinstance (). Add ( This, request, CallBack, Nohttp_login,true,false,true); -}Else { -Userinforequest request =Newuserinforequest (Constants.logout, requestmethod.get); -Callserver.getinstance (). Add ( This, request, CallBack, Nohttp_logout,true,false,true); -         } -     } in  -     PrivateHttpcallback<userinfo> CallBack =NewHttpcallback<userinfo>() { to  + @Override -          Public voidOnsucceed (intWhat, response<userinfo>response) { theUserInfo info = response.Get(); *             if(What = = Nohttp_login) {//working with login results $                 if(Info.issucceed ()) {Panax NotoginsengTvresult.settext ("Login Interface Results:"+info.getdata ()); -                 } the}Else if(What = = Nohttp_logout) {//handling Log Out results +                 if(Info.issucceed ()) { ATvresult.settext ("Exit interface Result:"+info.getdata ()); the                 } +             } -         } $  $ @Override -          Public voidOnfailed (intWhat, String URL, Object tag, Exception Exception,intResponsecode,LongNetworkmillis) { -Tvresult.settext ("request failed"); the         } -     };Wuyi}

Nohttp Package--02

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.