摘要:本文记录学习java与服务器通信的内容。
1.java与服务器通信原理
android端通过http请求(Request),发送到tomcat服务器端的servlet服务程序,并返回respond。
demo说明:
select端代码:
1)loginServlet.java
package test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class h
*/
@WebServlet("/h")
public class h extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public h() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("username");
String pass = request.getParameter("pass");
response.setContentType("text/html;charset=utf-8");
if("abc".equals(name)&&"123".equals(pass)) {
response.getWriter().print("登录成功");
}else {
response.getWriter().print("用户名密码错误");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
2)配置web.xml
Android端的代码
1)请求端,点击按钮,发送请求并获取返回数据显示出来
其中:url为服务请求的地址,例如url="http://My ip:8080/MyServlet/MyLogin" //包括了请求的哪一个服务
bt_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(){
@Override
public void run() {
super.run();
name = edt_name.getText().toString();
pass = edt_password.getText().toString();
String tempUrl = urlPath+"?username="+name+"&pass="+pass;
// String tempUrl = urlPath;
try {
URL url = new URL(tempUrl);
HttpURLConnection connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod("GET");
// connect.setConnectTimeout(1000);
int code = connect.getResponseCode();//返回数据状态代码
if(code == 200){//请求成功
InputStream inputStream = connect.getInputStream();
String result = Utils.getStringFromStream(inputStream);
showToast(result);//自定义函数,可以启动主线程改变UI
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
});
2) 自定义类Utils:获取返回的数据流,转化为字符串
class Utils.java
package com.example.id_test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Utils {
public static String getStringFromStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream baso = new ByteArrayOutputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = inputStream.read(buffer))!=-1){
baso.write(buffer,0,len);
}
inputStream.close();
byte[] byteArray = baso.toByteArray();
return new String(byteArray);
}
}
3)AndroidManifest.xml
添加网络权限
PS:完成以上内容时,有可有可能出现连接失败的情况:
Cleartext HTTP traffic to xxx not permitted
则解决问题 https://blog.csdn.net/MRYZJ/article/details/103003958
2.测试:
输入正确的用户和姓名,点击则显示登陆成功,否侧用户名或密码错误