Android uses third-party okhttp for login requests

Source: Internet
Author: User

Required third-party libraries: https://yun.baidu.com/share/link?shareid=3540318765&uk=1697368809

Structure of the POST request:

Create an Okhttpclient object
Okhttpclient client=new okhttpclient ();
URL address
String url= "Http://10.200.20.164:8080/OrderManager/login";
Carrying parameters
Formencodingbuilder builder=new Formencodingbuilder ();

Request Request = new Request.builder (). URL (URL). Post (Builder.build ())
. build ();

Client.newcall (Request) Enqueue (new Callback () {

/Interface Call succeeded
public void Onresponse (Response arg0) throws IOException {}

Interface call failed
public void OnFailure (Request arg0, IOException arg1) {}

});

login POST request key code :

Create a Okhttpclient object
Okhttpclient client = new Okhttpclient ();
URL address
String url = "URL address";
Post Add parameter
Formencodingbuilder builder = new Formencodingbuilder ();
Builder.add ("LoginName", name);
Builder.add ("loginpasswd", Psword);
Builder.add ("DeptNo", "001");
Request Request = new Request.builder (). URL (URL). Post (Builder.build ())
. build ();

Client.newcall (Request) Enqueue (new Callback () {

@Override

interface Invocation Succeeded
public void Onresponse (Response arg0) throws IOException {
0--success, 2--failure, abnormal
try {
Jsonobject Jo = new Jsonobject (Arg0.body (). String ());
int ret = Jo.getint ("ret");//Determine if the return field succeeded or failed
if (ret = = 1) {
Handler.sendemptymessage (0);
} else if (ret==-1) {
Handler.sendemptymessage (1);
}

} catch (Jsonexception e) {
E.printstacktrace ();
}
}

@Override

Interface call failed
public void OnFailure (Request arg0, IOException arg1) {

Handler.sendemptymessage (2);
}
});

Handler Handler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case 0:
Intent Intent = new Intent (Loginactivity.this,
Mainactivity.class);
Toast.maketext (Getapplicationcontext (), "Login Successful", Toast.length_short). Show ();
StartActivity (Intent);
Finish ();
Break
Case 1:
Toast.maketext (Getapplicationcontext (), "Login Failed", Toast.length_short). Show ();
Break
Case 2:
Toast.maketext (Getapplicationcontext (), "Network exception, check network", Toast.length_short). Show ();
Break
Default
Break
}
}

};

Android uses third-party okhttp for login requests

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.