很多朋友在咨询安卓端如何调用后端接口,我这里准备了两种接口,一种说java写的后端接口,一种是asp.net (C#)写的后端接口。
数据库有mysql的,也有sql server的。
先看效果:
我们以录入健康数据为例讲解,包括把数据从安卓端的表单中获取出来,调用后端接口,然后把数据保存到数据库。然后我们通过查询数据库,通过调用后端接口,把数据查询出来,显示在安卓端的列表中,点击列表还能查看明细,在安卓端要做一个数据列表,还是比较复杂的,对于初学者,还是需要耐心学习下,当然,我们还有视频教程 需要视频的加Q:1416759661。
然后上代码:
安卓端布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddDataActivity">
<EditText
android:id="@+id/tbusername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="张三"
android:hint="姓名"
></EditText>
<EditText
android:id="@+id/tbidcard"
android:text="510110000000000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="身份证"
android:layout_below="@+id/tbusername"
></EditText>
<EditText
android:id="@+id/tbtemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="体温"
android:text="36.2"
android:layout_below="@+id/tbidcard"
></EditText>
<EditText
android:id="@+id/tbbp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="血压"
android:text="85/115"
android:layout_below="@+id/tbtemp"
></EditText>
<EditText
android:id="@+id/tbrate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="心率"
android:text="72"
android:layout_below="@+id/tbbp"
></EditText>
<Button
android:id="@+id/btnsave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textSize="18sp"
android:layout_below="@+id/tbrate"
></Button>
</RelativeLayout>
添加健康数据的安卓端代码
package com.yyjcw.hdsys;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class AddDataActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_data);
Button btnsave=findViewById(R.id.btnsave);
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username=((EditText)findViewById(R.id.tbusername)).getText().toString();
String idcard=((EditText)findViewById(R.id.tbidcard)).getText().toString();
String temp=((EditText)findViewById(R.id.tbtemp)).getText().toString();
String bp=((EditText)findViewById(R.id.tbbp)).getText().toString();
String rate=((EditText)findViewById(R.id.tbrate)).getText().toString();
String urlstr="http://192.168.0.110:7766/hdapi.ashx";
try {
username=URLEncoder.encode(username,"utf-8");
idcard=URLEncoder.encode(idcard,"utf-8");
temp=URLEncoder.encode(temp,"utf-8");
bp=URLEncoder.encode(bp,"utf-8");
rate=URLEncoder.encode(rate,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String paramstr="rnum=1&username="+username+"&idcard="+idcard+"&temp="+temp+"&bp="+bp+"&rate="+rate;
sendHttp(urlstr,paramstr);
}
});
}
/*
* 功能:用于发起http请求,并得到返回的结果
* urlStr:请求的地址。
* paramStr:请求的参数 多个参数的格式:rnum=1&p1=a&p2=b
* */
private void sendHttp(final String urlStr, final String paramStr){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
OutputStream outputStream = conn.getOutputStream();
DataOutputStream out = new DataOutputStream(outputStream);
out.writeBytes(paramStr);
out.flush();
out.close();
InputStream inputStream = conn.getInputStream();
String res=FilesUtils.readInfo(inputStream);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(),"保存成功!",Toast.LENGTH_SHORT).show();
}
});
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DataListActivity">
<ListView
android:id="@+id/lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tblist"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:id="@+id/tv_time"
android:width="200dp"
android:layout_height="40dp"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/tv_username"
android:width="120dp"
android:layout_height="40dp"
android:gravity="center_vertical"
/>
<TextView
android:id="@+id/tv_temp"
android:width="120dp"
android:layout_height="40dp"
android:gravity="center_vertical"
/>
</TableRow>
</TableLayout>
package com.yyjcw.hdsys;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataListActivity extends AppCompatActivity {
private List<Map<String,Object>> list;
private ListView lvlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_list);
list=new ArrayList<Map<String,Object>>();
Map<String,Object> map =new HashMap<String,Object>();
map.put("id","0");
map.put("ctime","时间");
map.put("username","姓名");
map.put("temp","体温");
list.add(map);
lvlist=findViewById(R.id.lv_list);
lvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
HashMap<String, Object> map=(HashMap<String, Object>)lvlist.getItemAtPosition(i);
String itemid=map.get("id").toString();
if(!itemid.equals("0"))
{
Intent intent1=new Intent(DataListActivity.this,DetailActivity.class);
intent1.putExtra("itemid",itemid);
startActivity(intent1);
}
}
});
String userstr="http://192.168.0.110:7766/hdapi.ashx";
String paramstr="rnum=2";
sendHttp(userstr,paramstr);
}
/*
* 功能:用于发起http请求,并得到返回的结果
* urlStr:请求的地址。
* paramStr:请求的参数 多个参数的格式:rnum=1&p1=a&p2=b
* */
private void sendHttp(final String urlStr, final String paramStr){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestMethod("POST");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
OutputStream outputStream = conn.getOutputStream();
DataOutputStream out = new DataOutputStream(outputStream);
out.writeBytes(paramStr);
out.flush();
out.close();
InputStream inputStream = conn.getInputStream();
String res=FilesUtils.readInfo(inputStream);
try {
JSONArray arr = new JSONArray(res);
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = (JSONObject) arr.get(i);
String id = obj.getString("id");
String username = obj.getString("username");
String temp = obj.getString("temp");
String ctime = obj.getString("ctime");
Map<String,Object> map =new HashMap<String,Object>();
map.put("id",id);
map.put("username",username);
map.put("temp",temp);
map.put("ctime",ctime);
list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
MyAdapter adapter=new MyAdapter(DataListActivity.this, list);
lvlist.setAdapter(adapter);
}
});
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
package com.yyjcw.hdsys;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<Map<String, Object>> list;
public MyAdapter(Context context, List<Map<String, Object>> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(context).inflate(R.layout.itemcust, null);
final TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
final TextView tv_username= (TextView) view.findViewById(R.id.tv_username);
final TextView tv_temp = (TextView) view.findViewById(R.id.tv_temp);
tv_username.setText(list.get(i).get("username").toString());
tv_temp.setText(list.get(i).get("temp").toString());
tv_time.setText(list.get(i).get("ctime").toString());
return view;
}
}
项目代码下载 也可以加扣扣:1416759661