依赖
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'org.greenrobot:eventbus:3.0.0'
OkHttpUtils
public class OkHttpUtils {
private static OkHttpUtils instance;
private final OkHttpClient mOkHttpClient;
private final Handler mHandler;
public OkHttpUtils(){
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
mHandler = new Handler(Looper.getMainLooper());
}
public static OkHttpUtils getInstance(){
if (instance == null){
synchronized (OkHttpUtils.class){
if (instance == null){
instance = new OkHttpUtils();
}
}
}
return instance;
}
/**
* get
* @param url
* @param iCallBack
*/
public void doGet(String url, final ICallBack iCallBack){
Request request = new Request.Builder()
.url(url).build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
if (iCallBack != null){
mHandler.post(new Runnable() {
@Override
public void run() {
iCallBack.failed(e);
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()){
final String json = response.body().string();
if (iCallBack != null){
mHandler.post(new Runnable() {
@Override
public void run() {
iCallBack.success(json);
}
});
}
}
}
});
}
/**
* post
* @param url
* @param map
* @param iCallBack
*/
public void doPost(String url, Map<String,String> map , final ICallBack iCallBack){
FormBody.Builder builder = new FormBody.Builder();
if (map != null){
for (String key : map.keySet()){
builder.add(key,map.get(key));
}
}
FormBody formBody = builder.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Log.i("yz",e.getMessage());
if (iCallBack != null){
mHandler.post(new Runnable() {
@Override
public void run() {
iCallBack.failed(e);
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response != null && response.isSuccessful()){
final String json = response.body().string();
if (iCallBack != null){
mHandler.post(new Runnable() {
@Override
public void run() {
iCallBack.success(json);
}
});
}
}
}
});
}
}
接口
public interface ICallBack {
void success(String json);
void failed(Exception e);
}
Login布局
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/qq"
android:layout_gravity="center"
android:layout_marginTop="40dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:padding="10dp">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号 "
android:textSize="20sp"
android:layout_centerVertical="true"/>
<EditText
android:id="@+id/edit_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码 "
android:textSize="20sp"
android:layout_centerVertical="true"/>
<EditText
android:id="@+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text1"
android:layout_marginLeft="10dp"
android:password="true"/>
</RelativeLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="登录"
android:background="@drawable/btn_style"
android:textSize="20sp"
android:textColor="#fff" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="15dp">
<TextView
android:id="@+id/text_regist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户"
android:textSize="15sp"
android:textColor="#2676ed"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
Login逻辑代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String LOGIN_URL = "http://www.zhaoapi.cn/user/login";
private EditText edit_account;
private EditText edit_password;
private Button btn_login;
private TextView text_regist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
EventBus.getDefault().register(this);
}
private void initView() {
edit_account = (EditText) findViewById(R.id.edit_account);
edit_password = (EditText) findViewById(R.id.edit_password);
btn_login = (Button) findViewById(R.id.btn_login);
text_regist = (TextView) findViewById(R.id.text_regist);
btn_login.setOnClickListener(this);
text_regist.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
String account_login = edit_account.getText().toString().trim();
String password_login = edit_password.getText().toString().trim();
Map<String , String> map = new HashMap<>();
map.put("mobile", account_login);
map.put("password", password_login);
OkHttpUtils.getInstance().doPost(LOGIN_URL, map, new ICallBack() {
@Override
public void success(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String msg = jsonObject.getString("msg");
Toast.makeText(MainActivity.this,""+msg,Toast.LENGTH_SHORT).show();
if (msg.equals("登录成功")){
Intent intent = new Intent(MainActivity.this,ShowActivity.class);
startActivity(intent);
finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failed(Exception e) {
Toast.makeText(MainActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.text_regist:
Intent intent = new Intent(MainActivity.this,RegistActivity.class);
startActivity(intent);
break;
}
}
@Subscribe(threadMode = ThreadMode.MAIN )
public void eventBus(EventBusMsg busMsg){
edit_account.setText(busMsg.getAccount());
Log.i("yz",busMsg.getAccount());
edit_password.setText(busMsg.getPassword());
Log.i("yz",busMsg.getPassword());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().removeAllStickyEvents();
EventBus.getDefault().unregister(this);
}
}
注册布局
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/qq"
android:layout_gravity="center"
android:layout_marginTop="40dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:padding="10dp">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号 "
android:textSize="20sp"
android:layout_centerVertical="true"/>
<EditText
android:id="@+id/edit_account_regist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码 "
android:textSize="20sp"
android:layout_centerVertical="true"/>
<EditText
android:id="@+id/edit_password_regist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text1"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
<Button
android:id="@+id/btn_regist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="注册"
android:background="@drawable/btn_style"
android:textSize="20sp"
android:textColor="#fff" />
</LinearLayout>
注册逻辑
public class RegistActivity extends AppCompatActivity implements View.OnClickListener {
public static final String REGIST_URL = "http://www.zhaoapi.cn/user/reg";
private EditText edit_account_regist;
private EditText edit_password_regist;
private Button btn_regist;
private String account_regist;
private String password_regist;
private EventBusMsg mEventBusMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regist);
initView();
}
private void initView() {
edit_account_regist = (EditText) findViewById(R.id.edit_account_regist);
edit_password_regist = (EditText) findViewById(R.id.edit_password_regist);
btn_regist = (Button) findViewById(R.id.btn_regist);
btn_regist.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_regist:
//获取到用户输入的信息
account_regist = edit_account_regist.getText().toString().trim();
password_regist = edit_password_regist.getText().toString().trim();
Map<String , String> map = new HashMap<>();
map.put("mobile", account_regist);
map.put("password", password_regist);
OkHttpUtils.getInstance().doPost(REGIST_URL, map, new ICallBack() {
@Override
public void success(String json) {
try {
JSONObject jsonObject = new JSONObject(json);
String msg = jsonObject.getString("msg");
Toast.makeText(RegistActivity.this,""+msg,Toast.LENGTH_SHORT).show();
if (msg.equals("注册成功")){
EventBus.getDefault().post(new EventBusMsg(account_regist, password_regist));
finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failed(Exception e) {
Toast.makeText(RegistActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}
});
break;
}
}
}