This article shares the relevant code for Android using get login for your reference. The specific content is as follows
Tools
package .; import ; import ; import ; import ; import ; import ; import ; /** * Created by Administrator on 2016-03-27. */ public class HttpUtils { OkHttpClient client = new OkHttpClient(); public static final MediaType JSON = ("application/json; charset=utf-8"); public String login(String url, String json) throws IOException { //Convert the requested content string to json RequestBody body = (JSON, json); //RequestBody formBody = new FormEncodingBuilder() Request request = new () .url(url) .post(body) .build(); Response response = (request).execute(); String result = ().string(); return result; } public String bolwingJson(String username, String password) { return "{'username':" + username + "," + "'password':" + password + "}"; // "{'username':" + username + ","+"'password':"+password+"}"; } }
Activity
package .okhttpdemo3post; import .; import ; import ; import ; import ; import ; import ; import ; import ; import .; import org.; import ; public class MainActivity extends AppCompatActivity implements { private static final String TAG ="MainActivity" ; //username 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) { (savedInstanceState); setContentView(.activity_main); initView(); initListener(); } /** * Initialize the component */ private void initView() { mEtUsername = (EditText) findViewById(.login_et_name); mEtPwd = (EditText) findViewById(.login_et_pwd); mBtnLogin = (Button) findViewById(.login_btn_login); mTvResult = (TextView) findViewById(.login_tv_result); } /** * Set up the listener */ private void initListener() { (this); } /* Click event listening */ @Override public void onClick(View v) { if(v==mBtnLogin){ login(); } } /* Log in */ private void login() { final String username = ().toString().trim(); final String password = ().toString().trim(); if((username) || (password)){ (, "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 = (username, password); //String user ="{'username':" + username + ","+"'password':"+password+"}"; (TAG, "user:" + user); try { final String result = (url, user); (TAG, "result:" + result); //Update the UI, in the UI thread runOnUiThread(new Runnable() { @Override public void run() { if("SUCCESS".equals(result)){ ("Login successfully"); }else{ ("Login failed"); } } }); } catch (IOException e) { (); } } }.start(); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.