2、客户端通过socket或者httpurlconnection的方式进行连接访问
服务端:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/htmlcharset=utf-8")
req.setCharacterEncoding("utf-8")
resp.setCharacterEncoding("utf-8")
PrintWriter out = resp.getWriter()
//用HTML格式给浏览器返回数据
out.println("<html>")
out.println("<head>")
out.println("<title>Tomcat Servlet测试</title>")
out.println("</head>")
out.println("<body>")
out.println("Hello,First Servlet!")
out.println("</body>")
out.println("</html>")
out.println("Hello,第一个Tomcat!!!")
out.close()
}
客户端:
private String doGet(String url){
String responseStr = ""
try {
String name = nameEdit.getText().toString().trim()
String code = codeEdit.getText().toString().trim()
String getUrl = URL + "?NAME=" + name+"&"+"CODE=" + code
HttpGet httpRequest = new HttpGet(getUrl)
HttpParams params = new BasicHttpParams()
ConnManagerParams.setTimeout(params, 1000)
HttpConnectionParams.setConnectionTimeout(params, 3000)
HttpConnectionParams.setSoTimeout(params, 5000)
httpRequest.setParams(params)
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest)
final int ret = httpResponse.getStatusLine().getStatusCode()
if(ret == HttpStatus.SC_OK){
responseStr = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8)
}else{
responseStr = "-1"
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
return responseStr
}
首先你除了会安卓开发外要会javaweb或php等服务器语言。然后给服务器编写对数据库的操作和给app提供操作的接口,之后你的服务器连接数据库,你的app通过http等协议通过接口就能实现你想要的了。大概就是这样的流程,一般在公司中服务器的代码不需要你写。具体的不是三言两语就能做好的,网上搜搜大神们的写案例吧。欢迎分享,转载请注明来源:夏雨云
评论列表(0条)