Android uses okhttp (get way) to log in _android

Source: Internet
Author: User
Tags event listener gettext string to json

The example of this article for you to share the Android use of Get-way login code, for your reference, the specific content as follows

Tool Class

Package com.liunan.okhttpdemo3post.Utils;

Import java.io.IOException; Import OKHTTP3.
mediatype; Import OKHTTP3.
Okhttpclient; Import OKHTTP3.
Request; Import OKHTTP3.
Requestbody; Import OKHTTP3.
Response; Import OKHTTP3.

Responsebody;
 /** * Created by the Administrator on 2016-03-27.
  * * public class Httputils {okhttpclient client = new Okhttpclient (); public static final MediaType JSON = Mediatype.parse ("Application/json;

  Charset=utf-8 "); public string login (string URL, string json) throws IOException {//Convert the requested content string to json requestbody BODY = Requestbod
    Y.create (JSON, JSON);
        Requestbody formbody = new Formencodingbuilder () request = new Request.builder (). URL (URL)

    . Post (body).

    Response Response = client.newcall (Request). Execute ();


    string result = Response.body (). String ();


  return result; public string Bolwingjson (string username, string password) {return "{' username ':" + username + "," + "' Password ':" + password + "}";
  "{' username ':" + username + "," + "' Password ':" +password+ "}"; 

 }


}

Activity

Package com.liunan.okhttpdemo3post;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.text.TextUtils;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;

Import Android.widget.Toast;

Import Com.liunan.okhttpdemo3post.Utils.HttpUtils;

Import Org.w3c.dom.Text;

Import java.io.IOException; public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private static final String TAG = "
  Mainactivity ";
  User name private EditText metusername;
  Password private edittext metpwd;
  Login button Private button mbtnlogin;

  Private TextView Mtvresult;

  Private String URL = "Http://192.168.1.102:8080/Login/login";
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.activity_main);
    Initview ();
  Initlistener (); }/** * Initialization component */private void Initview () {metusername = (edittext) Findviewbyid (r.id.login_et_name);

    Metpwd = (edittext) Findviewbyid (R.ID.LOGIN_ET_PWD);


    Mbtnlogin = (Button) Findviewbyid (R.id.login_btn_login);

  Mtvresult = (TextView) Findviewbyid (R.id.login_tv_result);


  /** * Set listener/private void Initlistener () {Mbtnlogin.setonclicklistener (this);
    /* Click event Listener/@Override public void OnClick (View v) {if (v==mbtnlogin) {login ();
    }/* Login/private void login () {final String username = Metusername.gettext (). toString (). Trim ();


    Final String password = Metpwd.gettext (). toString (). Trim (); if (Textutils.isempty (username) | |
      Textutils.isempty (password)) {Toast.maketext (Mainactivity.this, "username or password cannot be empty", Toast.length_short). Show ();
    Return
          New Thread () {@Override public void run () {httputils httputils = new Httputils (); Convert to json String user = Httputils.bolWingjson (username, password);

        String user = "{' username ':" + username + "," + "' Password ':" +password+ "}";

        LOG.D (TAG, "User:" + user);
          Try {final String result = Httputils.login (URL, user);
          LOG.D (TAG, "results:" + result);
              Update UI, Runonuithread in UI thread (new Runnable () {@Override public void run () {

              if ("SUCCESS". Equals (Result)) {Mtvresult.settext ("login succeeded");
              }else{mtvresult.settext ("Login Failed");
        }
            }
          });
        catch (IOException e) {e.printstacktrace ();


  }}.start ();

 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.