安卓客户端代码:
安卓向服务器发送请求有两种形式,一种是url一种是json。这里我们采用url
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.register:
String username = userName.getText().toString();
String userpassword = userPassword.getText().toString();
String sex = "";
if(sex_male.isChecked()){
sex = "男";
}else{
sex = "女";
}
String academic = spinner.getSelectedItem().toString();
String address = "http://192.168.2.113:8080/RegisterTest/RegisterServlet?account=" + username + "&password=" + userpassword
+ "&sex=" + sex + "&academic=" + academic;
HttpUtil.sendHttpRequest(address, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "数据提交服务器失败!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "注册成功!", Toast.LENGTH_SHORT).show();
}
});
}
});
break;
case R.id.login:
String usernamel = userName.getText().toString();
String userpasswordl = userPassword.getText().toString();
String addressl = "http://192.168.2.113:8080/RegisterTest/LoginServlet?account=" + usernamel + "&password=" + userpasswordl;
HttpUtil.sendHttpRequest(addressl, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(response.toString().substring(106));
Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show();
}
});
}
});
};
}
安卓源码地址:http://pan.baidu.com/s/1dEUQ0N3