Android使用okHttp(get方式)登录
本文实例为大家分享了Android使用get方式登录的相关代码,供大家参考,具体内容如下
工具类
packagecom.liunan.okhttpdemo3post.Utils; importjava.io.IOException; importokhttp3.MediaType; importokhttp3.OkHttpClient; importokhttp3.Request; importokhttp3.RequestBody; importokhttp3.Response; importokhttp3.ResponseBody; /** *CreatedbyAdministratoron2016-03-27. */ publicclassHttpUtils{ OkHttpClientclient=newOkHttpClient(); publicstaticfinalMediaTypeJSON =MediaType.parse("application/json;charset=utf-8"); publicStringlogin(Stringurl,Stringjson)throwsIOException{ //把请求的内容字符串转换为json RequestBodybody=RequestBody.create(JSON,json); //RequestBodyformBody=newFormEncodingBuilder() Requestrequest=newRequest.Builder() .url(url) .post(body) .build(); Responseresponse=client.newCall(request).execute(); Stringresult=response.body().string(); returnresult; } publicStringbolwingJson(Stringusername,Stringpassword){ return"{'username':"+username+","+"'password':"+password+"}"; //"{'username':"+username+","+"'password':"+password+"}"; } }
Activity
packagecom.liunan.okhttpdemo3post; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.text.TextUtils; importandroid.util.Log; importandroid.view.View; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.TextView; importandroid.widget.Toast; importcom.liunan.okhttpdemo3post.Utils.HttpUtils; importorg.w3c.dom.Text; importjava.io.IOException; publicclassMainActivityextendsAppCompatActivityimplementsView.OnClickListener{ privatestaticfinalStringTAG="MainActivity"; //用户名 privateEditTextmEtUsername; //密码 privateEditTextmEtPwd; //登录按键 privateButtonmBtnLogin; privateTextViewmTvResult; privateStringurl="http://192.168.1.102:8080/Login/login"; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } /** *初始化组件 */ privatevoidinitView(){ 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); } /** *设置监听器 */ privatevoidinitListener(){ mBtnLogin.setOnClickListener(this); } /* 单击事件监听 */ @Override publicvoidonClick(Viewv){ if(v==mBtnLogin){ login(); } } /* 登录 */ privatevoidlogin(){ finalStringusername=mEtUsername.getText().toString().trim(); finalStringpassword=mEtPwd.getText().toString().trim(); if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){ Toast.makeText(MainActivity.this,"用户名或者密码不能为空",Toast.LENGTH_SHORT).show(); return; } newThread(){ @Override publicvoidrun(){ HttpUtilshttpUtils=newHttpUtils(); //转换为JSON Stringuser=httpUtils.bolwingJson(username,password); //Stringuser="{'username':"+username+","+"'password':"+password+"}"; Log.d(TAG,"user:"+user); try{ finalStringresult=httpUtils.login(url,user); Log.d(TAG,"结果:"+result); //更新UI,在UI线程中 runOnUiThread(newRunnable(){ @Override publicvoidrun(){ if("SUCCESS".equals(result)){ mTvResult.setText("登录成功"); }else{ mTvResult.setText("登录失败"); } } }); }catch(IOExceptione){ e.printStackTrace(); } } }.start(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。