Open the Android Studio, choose File-> New-> New Project
Select Empty Activity, click Next
Fill in the project name, package name, path and other information, click Finish
Add rely on bulid.gradle (app) file
implementation 'com.squareup.okhttp:okhttp:2.4.0'
implementation 'com.alibaba:fastjson:1.2.10'
Click Sync Now
Permission to join the network in the AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
Add a Button control and a ListView in the layout file activity_main.xml, we want to click on the Button all students can achieve access to information
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/bt_refresh"
android:text="获取学生信息"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Open the Design view, something like this
Each piece of information we are again in the new layout folder layout file text.xml, for displaying queried
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textAlignment="center"
android:layout_weight="1"
android:text="id"
android:textSize="30sp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textAlignment="center"
android:layout_weight="1"
android:text="name"
android:textSize="30sp"/>
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textAlignment="center"
android:layout_weight="1"
android:text="sex"
android:textSize="30sp"/>
</LinearLayout>
Open the Design view, something like this
New Student entity class, provided that get and set methods
public class Student implements Serializable {
private int id;
private String sex;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
Following variables are defined (MainActivity complete code is at the end) in the MainActivity
Get the current Context and layout files for each component Button to add a listener, after the implementation of getData Press () method to get student information
In the method using getJsonData okhttp json acquired data, and then parses the data into a Student array, and the information is displayed in the ListView with a Handler
Then we define this Handler, it is our custom to control ListView Adapter
Then define MyAdapter
Well, now run the spring-boot our previous project, then open the virtual machine running this app android
Click "Get Student Information" button to get to our database of student information
There are arguments for the request (insert here take to give chestnuts, suppose we insert new student information filled out in the stu)
To parse json, our server side also like to add a dependent fastjson
server code
Here posted MainActivity code, but it is recommended that knock yourself again
public class MainActivity extends AppCompatActivity {
private ListView mLv;
private Context mContext;
private String urlStr = "http://IP地址:8080/student";
private List<Student> studentList = null;
private Button getInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLv = findViewById(R.id.list_item);
getInfo = findViewById(R.id.bt_refresh);
getInfo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//获取学生信息列表
getJsonData();
//插入信息
/*Student stu = new Student();
stu.setId(12121);
stu.setName("赵九");
stu.setSex("男");
insertInto(stu);*/
}
});
mContext = this;
}
private void getJsonData(){
OkHttpClient mOkHttpClient = new OkHttpClient();
final Request request = new Request.Builder().url(urlStr).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.v("Fail",e.getMessage());
}
@Override
public void onResponse(Response response) throws IOException {
String responseStr = response.body().string();
List<Student> studentEntities = new ArrayList<>();
studentEntities = com.alibaba.fastjson.JSONArray.parseArray(responseStr,Student.class);
Message msg = mHandler.obtainMessage();
msg.obj = studentEntities;
mHandler.sendMessage(msg);
}
});
}
String INSERT_STUDENT = "http://192.168.43.140:8080/insertstudent";
/**
* 插入单个student到mysql
* **/
private void insertInto(Student stu){
String stuStr = JSON.toJSONString(stu);
OkHttpClient mOkHttpClient = new OkHttpClient();
FormEncodingBuilder builder = new FormEncodingBuilder();
builder.add("stu",stuStr);
final Request request = new Request.Builder()
.url(INSERT_STUDENT)
.post(builder.build())
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.v("Fail",e.getMessage());
}
@Override
public void onResponse(Response response) throws IOException {
}
});
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
studentList = (List<Student>)msg.obj;
if(null != studentList){
mLv.setVisibility(View.VISIBLE);
MyAdapter adapter = new MyAdapter(mContext,studentList);
mLv.setAdapter(adapter);
}
}
};
private class MyAdapter extends BaseAdapter {
private List<Student> studentEntities = new ArrayList<>();
private Context context;
public MyAdapter(Context context,List<Student> studentEntities){
this.context = context;
this.studentEntities = studentEntities;
}
@Override
public int getCount() {
if(studentEntities.size() != 0){
return studentEntities.size();
}else{
return 0;
}
}
@Override
public Object getItem(int i) {
if(studentEntities.size() != 0){
return studentEntities.get(i);
}else{
return null;
}
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder mViewHolder = null;
if(null == view){
mViewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.text,null);
mViewHolder.mTvName = (TextView) view.findViewById(R.id.name);
mViewHolder.mTvId = (TextView) view.findViewById(R.id.id);
mViewHolder.mTvSex = (TextView) view.findViewById(R.id.sex);
view.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) view.getTag();
}
Student stu = studentEntities.get(i);
mViewHolder.mTvName.setText(stu.getName());
mViewHolder.mTvId.setText(String.valueOf(stu.getId()));
mViewHolder.mTvSex.setText(stu.getSex());
return view;
}
}
private class ViewHolder{
private TextView mTvName,mTvId,mTvSex;
}
}