由于最近帮助一个学弟做了一个小的系统,需要用到客户端和服务器来实现通信的效果。然后我这边也是第一次做这样东西,所以我就把这几天遇到的问题在这里做下总结,也可以方便后面的网友们看看。
我们在通讯过程中客户端可以通过post和get两种方式来与服务端进行通信。我在这边就介绍下post方式吧。
1、客户端
public static JSONObject sendState(JSONObject json, String url) {InputStreamReader isr = null;DataOutputStream dos = null;HttpURLConnection urlConn = null;BufferedReader bufferedReader = null;JSONObject jsonObject = null;try {URL url1 = new URL(url);urlConn = (HttpURLConnection) url1.openConnection();urlConn.setDoInput(true);//设置输入流采用字节流urlConn.setDoOutput(true);//设置输出流采用字节流urlConn.setRequestMethod(\"POST\");//post方法urlConn.setUseCaches(false);//缓存urlConn.setReadTimeout(20000);urlConn.setConnectTimeout(20000);urlConn.setRequestProperty(\"Content-Type\", \"application/x-www-form-urlencoded\");//设置meta参数urlConn.setRequestProperty(\"Charset\", \"utf-8\");//缓存String data = json.toString();urlConn.connect();//连接服务器发送消息if(data!=null&&!\"\".equals(data)){dos = new DataOutputStream(urlConn.getOutputStream());dos.write((json.toString()).getBytes());//写数据dos.close();dos.flush();}if(urlConn.getResponseCode()==200){isr = new InputStreamReader(urlConn.getInputStream());bufferedReader = new BufferedReader(isr);String result = \"\";String readLine = null;while ((readLine = bufferedReader.readLine()) != null) {result += readLine;}jsonObject = new JSONObject(result);bufferedReader.close();isr.close();}urlConn.disconnect();//关闭连接return jsonObject;} catch (MalformedURLException e) {e.printStackTrace();return jsonObject;} catch (IOException e) {e.printStackTrace();return jsonObject;} catch (JSONException e) {e.printStackTrace();return jsonObject;}
我们可以将用来查询的Sql语句所需要的数据都存放到json中上传给服务器,例如登录功能,我们将用户名、密码写入json中。
2、服务端
public class Login extends HttpServlet{
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 解决中文乱码
request.setCharacterEncoding(\”UTF-8\”);
response.setContentType(\”text/html;charset=UTF-8\”);
//response.setCharacterEncoding(\”UTF-8\”);
PrintWriter out = response.getWriter();
BufferedReader in = new BufferedReader(new InputStreamReader(
request.getInputStream(),\”utf-8\”));//这里很重要,中文乱码可以解决。
StringBuilder sb = new StringBuilder();
String line = null;
String result = \”\”;
JSONObject jsonObject = null;
while ((line = in.readLine()) != null) {
sb.append(line);
}
result = sb.toString();
jsonObject = JSONObject.fromObject(result);
String username = jsonObject.getString(\”username\”);
String password = jsonObject.getString(\”password\”);
UserServiceImpl impl = new UserServiceImpl();
boolean isExit = impl.queryUserForLogin(username, password);
Map<String, String> params = new HashMap<String, String>();
if(isExit) {
params.put(\”result\”, \”success\”);
} else {
params.put(\”result\”, \”fail\”);
}
JSONObject jsonObjec = JSONObject.fromObject(params);
out.write(jsonObjec.toString());
out.flush();
out.close();
}
}
我们在拿到用户名密码的数据后调用sql查询,将结果返回给客户端即可实现登录的功能。
以前没写过以为很难,通过这次编写这个系统学到了很多东西。看来多弄弄新东西还是很不错的体验啊。