Eclipse开发简易的Android服务器
来源:互联网 发布:java微信后台开发 编辑:程序博客网 时间:2024/06/06 02:42
android 程序猿搭建一个简单的服务器后台:
1、环境配置:
(1)、Eclipse (纯净版需安装ee插件)
提示:Help->Install new software->work with: Juno - http://download.eclipse.org/releases/juno(取消PHP相关的勾选)
(2)、配置Web.xml
新增:
1、环境配置:
(1)、Eclipse (纯净版需安装ee插件)
提示:Help->Install new software->work with: Juno - http://download.eclipse.org/releases/juno(取消PHP相关的勾选)
(2)、安装Apache-tomact
2、新建Web项目:
提示:File->new->other->Dynamic Web Project->Next
(1)、填写项目名
(2)、Target Runtime 点击->Apache Tomcat v7.0 ->DownLoad and Install->finish
项目生成如图:
3、新建服务器Java类:
(1)、Java Resources目录下新建:MyHttpServlet
public class MyHttpServlet extends HttpServlet {public MyHttpServlet() { super(); } public void destroy() { super.destroy(); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); username=new String (username.getBytes("ISO8859-1"),"UTF-8"); password=new String (password.getBytes("ISO8859-1"),"UTF-8"); System.out.println("姓名"+username); System.out.println("密码"+password); if(username.equals("GR")&&password.equals("8888")){ //默认情况下使用的是iso8859——1编码,但是如果发现码表中没有当前字符,会使用当前系统下默认编码:GBK response.getOutputStream().write("登录成功".getBytes("utf-8")); }else{ response.getOutputStream().write("登录失败".getBytes("UTF-8")); } } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
(2)、配置Web.xml
新增:
<servlet><servlet-name>test</servlet-name><servlet-class>io.gr.ServerDemo.MyHttpServlet</servlet-class>//文件路径</servlet><servlet-mapping><servlet-name>test</servlet-name><url-pattern>/Servers</url-pattern>//这里可以自定义</servlet-mapping>
(3)、双击 Tomcat v7.0 Server at localhost :
(4)、右键 Tomcat v7.0 Server at localhost,点击Add and Remove —— Add——Finish
(5)、运行测试:
点击:
运行结果:
(6)、请求测试:
打开浏览器,输入:
本地IP+端口号+/项目名+/映射名(即Web.xml中的映射名)+?+参数。。。。
例如:http://127.0.0.1:8080/SimpleAndroidServerDemo/Servers?username=GR&password=8888
请求成功:
Eclipse服务器接收:
4、创建Android项目:
这里就不过多介绍,代码如下:
import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;/** * Created by GR on 2017/4/7. */public class NetUilts { /* * 用post方式登录 * @param username * @param password * @return 登录状态 * */ public static String loginofPost(String username,String password){ HttpURLConnection conn=null; try { URL url=new URL("http://192.168.1.55:8080/SimpleAndroidServerDemo/Servers"); conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");//设置请求方式 conn.setConnectTimeout(10000);//设置连接超时时间 conn.setReadTimeout(5000);//设置读取超时时间 //POST请求的参数 OutputStream out=conn.getOutputStream();//获得输出流对象,用于向服务器写数据 String data="username="+username+"&"+"password="+password; out.write(data.getBytes());//向服务器写数据; out.close();//关闭输出流 conn.connect();//开始连接 int responseCode=conn.getResponseCode();//获取响应吗 if(responseCode==200){ //访问成功 InputStream is=conn.getInputStream();//得到InputStream输入流 String state=getstateFromInputstream(is); return state; }else{ //访问失败 } } catch (Exception e) { e.printStackTrace(); }finally{ if(conn!=null){//如果conn不等于空,则关闭连接 conn.disconnect(); } } return null; } /* * 使用GET的方式登录 * @param username * @param password * @return 登录状态 * */ public static String loginOfGet(String username,String password){ HttpURLConnection conn=null; try { String data="username="+username+"&"+"password="+password; URL url=new URL("http://192.168.1.55:8080/SimpleAndroidServerDemo/Servers?"+data); conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");//设置请求方式 conn.setConnectTimeout(10000);//设置连接超时时间 conn.setReadTimeout(5000);//设置读取超时时间 conn.connect();//开始连接 int responseCode=conn.getResponseCode();//获取响应吗 if(responseCode==200){ //访问成功 InputStream is=conn.getInputStream();//得到InputStream输入流 String state=getstateFromInputstream(is); Logger.i("请求结果:"+state); return state; }else{ //访问失败 Logger.i("访问失败!"); } } catch (Exception e) { e.printStackTrace(); Logger.i("访问异常!"); }finally{ if(conn!=null){//如果conn不等于空,则关闭连接 conn.disconnect(); } } return null; } private static String getstateFromInputstream(InputStream is) throws IOException { ByteArrayOutputStream baos=new ByteArrayOutputStream();//定义一个缓存流 byte[] buffer=new byte[1024];//定义一个数组,用于读取is int len=-1; while((len =is.read(buffer)) != -1){//将字节写入缓存 baos.write(buffer,0,len); } is.close();//关闭输入流 String state =baos.toString();//将缓存流中的数据转换成字符串 baos.close(); return state; }}
外部调用该方法即可。。。。(记得添加网络权限)
创建服务器主要是为了方便学习和开发自己App的功能调试
1 0
- Eclipse开发简易的Android服务器
- 使用Eclipse搭建简易Android服务器
- 自己动手开发简易的Web服务器
- 用vb.net开发的简易服务器
- Android sdk开发,eclipse和android studio打jar包的简易方法
- Android传感器的使用开发、简易指南针
- Android开发---简易好用的轮播器
- Android传感器的使用开发、简易指南针
- Eclipse的简易教程
- android开发简易浏览器
- Android开发的欢迎界面制作的简易教程
- 简易WWW服务器的架设
- 简易WWW服务器的架设
- 服务器文件的简易读写
- rtmp协议的简易服务器
- 一个简易的http服务器。
- 简易HTTP服务器的实现
- python版的简易服务器
- js截取中英文字符串长度
- Recyclerview的一些个人理解与使用(三)列表中的计时器
- lotus domino动态选择多个执行人
- SSE基本四则运算例子
- 1027. 打印沙漏(20)
- Eclipse开发简易的Android服务器
- Activity全方位了解,总有你不知道的一面
- 蓝桥杯-2017模拟赛本科试题-风险度量
- POJ
- Git与Github的使用学习
- 在原来的基础上添加了保存检测到的活体图片并且图片的保存形式为时间戳的方式
- Thinkphp3.2 伪静态以及thinphp3.2去掉index.php方法
- linux无名管道和有名管道
- 你应该知道的25道Javascript面试题