Android learning ---- Using okHttp (POST) --- login, Android okhttp
Tool
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 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 request content string to json RequestBody body = RequestBody. create (JSON, json); // RequestBody formBody = new FormEncodingBuilder () Request request = new Request. builder (). url (url ). post (body ). build (); Response response = client.newcall(requestcmd.exe cute (); 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"; // username private EditText mEtUsername; // password private EditText mEtPwd; // The logon key 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 ();}} /* log on */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, "the user name or password cannot be blank", 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, "result:" + result); // update the UI. In the UI thread, runOnUiThread (new Runnable () {@ Override public void run () {if ("SUCCESS ". equals (result) {mTvResult. setText ("Logon successful");} else {mTvResult. setText ("Logon Failed") ;}}} catch (IOException e) {e. printStackTrace ();}}}. start ();}}