一、页面
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background_2">
<View
android:layout_width="match_parent"
android:layout_height="150dp">
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginStart="120dp"
android:textSize="25dp"
android:layout_marginTop="10dp"
android:text="用户名:">
</TextView>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:textColor="#ff000000"
android:maxLength="13"
android:textSize="20sp">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginStart="120dp"
android:textSize="25dp"
android:layout_marginTop="10dp"
android:text="密 码:">
</TextView>
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:textColor="#ff000000"
android:maxLength="13"
android:inputType="textPassword"
android:textSize="20sp">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="250dp"
android:switchMinWidth="60dp"
android:background="@drawable/login_button"
android:textColor="#ff000000"
android:layout_marginTop="20dp"
android:width="100dp"
android:textSize="22dp"
android:text="登录">
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
res/drawable 里的背景 
二、逻辑代码
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.zilong.demo1.web.RestApi;
import com.zilong.demo1.web.result.RestResponse;
public class MainActivity extends Activity {
private Button next = null;
private EditText username = null;
private EditText pwd = null;
protected void onCreate(Bundle savedInstanceState){
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.login);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
username = (EditText) findViewById(R.id.username);
pwd = (EditText) findViewById(R.id.pwd);
RestResponse res = RestApi.login(username.getText().toString(), pwd.getText().toString());
if (res.getCode().equals(200)) {
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(MainActivity.this,FirstActivity.class);
startActivity(intent);
}
else {
Toast toast = Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}
import com.alibaba.fastjson.JSONObject;
import com.zilong.demo1.web.result.RestResponse;
import static com.zilong.demo1.constant.ApiConstant.BASE_URL;
public class RestApi {
public static RestResponse login(String username, String password) {
JSONObject json = new JSONObject();
json.put("username", username);
json.put("password", password);
return RestUtils.post(BASE_URL + "/login/test", String.valueOf(json));
}
}
import okhttp3.MediaType;
public class ApiConstant {
public static final Integer SC_OK_200 = 200;
public static final Integer SC_INTERNAL_SERVER_ERROR_500 = 500;
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public static final String BASE_URL = "http://127.0.0.1:8080/api";
}
import com.alibaba.fastjson.JSONObject;
import com.zilong.demo1.web.result.RestResponse;
import java.util.Map;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import static com.zilong.demo1.constant.ApiConstant.JSON;
public class RestUtils {
public static RestResponse get(String url, Map<String, String> params, String token) {
HttpUrl.Builder urlBuilder = HttpUrl.parse(url)
.newBuilder();
params.forEach((k, v) -> urlBuilder.addQueryParameter(k, v));
Request request = new Request.Builder()
.addHeader("token", token)
.url(urlBuilder.build())
.get()
.build();
try {
ResponseBody responseBody = RequestExecutor.execute(request);
System.out.println(responseBody.string());
return null;
} catch (Exception e) {
return RestResponse.error(e.getMessage());
}
}
public static RestResponse post(String url, String body) {
Request.Builder builder = new Request.Builder()
.url(url)
.post(RequestBody.create(JSON, body));
Request request = builder.build();
System.out.println("request: " + request);
try {
ResponseBody responseBody = RequestExecutor.execute(request);
return JSONObject.parseObject(responseBody.string(), RestResponse.class);
} catch (Exception e) {
e.printStackTrace();
return RestResponse.error(e.getMessage());
}
}
public static RestResponse put(String token) {
Request request = new Request.Builder().addHeader("token",token).build();
return null;
}
public static RestResponse delete(String token) {
Request request = new Request.Builder().addHeader("token",token).build();
return null;
}
}
import com.zilong.demo1.exception.RequestException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
public class RequestExecutor {
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(2, TimeUnit.MINUTES)
.build();
public static ResponseBody execute(Request request) throws RequestException {
try {
return okHttpClient.newCall(request).execute().body();
} catch (IOException e) {
e.printStackTrace();
throw new RequestException("请稍后尝试");
}
}
}
三、依赖
implementation "com.squareup.okhttp3:okhttp:3.12.2"
implementation "org.projectlombok:lombok:1.18.16"
implementation "com.alibaba:fastjson:1.2.72"
四、配置文件
manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zilong.demo1">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo1">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FirstActivity">
</activity>
</application>
</manifest>
res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
五、效果
 
|