Android使用okHttp(get方式)登錄
更新時間:2016年08月15日 16:55:26 作者:森林森
這篇文章主要為大家詳細(xì)介紹了Android使用okHttp(get方式)進(jìn)行登錄,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Android使用get方式登錄的相關(guān)代碼,供大家參考,具體內(nèi)容如下
工具類
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 {
//把請求的內(nèi)容字符串轉(zhuǎn)換為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(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" ;
//用戶名
private EditText mEtUsername;
//密碼
private EditText mEtPwd;
//登錄按鍵
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();
}
/**
* 初始化組件
*/
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);
}
/**
* 設(shè)置監(jiān)聽器
*/
private void initListener() {
mBtnLogin.setOnClickListener(this);
}
/*
單擊事件監(jiān)聽
*/
@Override
public void onClick(View v) {
if(v==mBtnLogin){
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, "用戶名或者密碼不能為空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(){
@Override
public void run() {
HttpUtils httpUtils = new HttpUtils();
//轉(zhuǎn)換為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, "結(jié)果:" + result);
//更新UI,在UI線程中
runOnUiThread(new Runnable() {
@Override
public void run() {
if("SUCCESS".equals(result)){
mTvResult.setText("登錄成功");
}else{
mTvResult.setText("登錄失敗");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 詳解Android中使用OkHttp發(fā)送HTTP的post請求的方法
- Android的OkHttp包中的HTTP攔截器Interceptor用法示例
- Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析
- Android M(6.x)使用OkHttp包解析和發(fā)送JSON請求的教程
- Android使用okHttp(get方式)下載圖片
- 使用Android的OkHttp包實(shí)現(xiàn)基于HTTP協(xié)議的文件上傳下載
- 詳解Android使用OKHttp3實(shí)現(xiàn)下載(斷點(diǎn)續(xù)傳、顯示進(jìn)度)
- 使用OkHttp包在Android中進(jìn)行HTTP頭處理的教程
- Android OkHttp的簡單使用和封裝詳解
- Android的OkHttp包處理用戶認(rèn)證的代碼實(shí)例分享
- Android-Okhttp的使用解析
相關(guān)文章
Android Webview上的ssl warning的處理方式詳解及實(shí)例
這篇文章主要介紹了Android Webview上的ssl warning的處理方式詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android實(shí)現(xiàn)底部彈出的對話框功能
這篇文章主要介紹了Android實(shí)現(xiàn)底部彈出的對話框功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
說說在Android如何使用服務(wù)(Service)的方法
這篇文章主要介紹了說說在Android如何使用服務(wù)(Service)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Android禁止EditText自動彈出軟鍵盤的方法及遇到問題
這篇文章主要介紹了Android禁止EditText自動彈出軟鍵盤的方法及遇到問題,需要的朋友可以參考下2018-07-07
如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
這篇文章主要給大家介紹了關(guān)于如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
基于SceneForm實(shí)現(xiàn)子彈射擊(繪制子彈運(yùn)行軌跡)
這篇文章主要為大家詳細(xì)介紹了基于SceneForm實(shí)現(xiàn)子彈射擊,繪制子彈運(yùn)行軌跡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11

