在tomact服务器中自定义web服务,模拟数据下载更新
1.开启tomcat
目录apache-tomcat-7.0.68instartup.bat
2.将apache-tomcat-7.0.68webapps目录下的WEB-INF拷贝到自定义的目录中,这样就可以访问自定义内容
如:拷贝到apache-tomcat-7.0.68myApp目录,就可以在浏览器中访问该目录下的text.txt文件
text.txt中写自定义数据:
如 版本2.0
3.开启网络下载数据,
注意:最好使用系统模拟器,端口号为10.0.2.2
setContentView(R.layout.activity_main); new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub String jsonByInternet = getJsonByInternet("http://10.0.2.2:8080/myApp/test.txt"); System.out.println(jsonByInternet); } }).start();
下载的代码:
public static String getJsonByInternet(String path){ try { URL url = new URL(path.trim()); //打开连接 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); System.out.println("连接中...."); if(200 == urlConnection.getResponseCode()){ System.out.println("连接成功...."); //得到输入流 InputStream is =urlConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while(-1 != (len = is.read(buffer))){ baos.write(buffer,0,len); baos.flush(); } return baos.toString(); } } catch (IOException e) { e.printStackTrace(); } return null; }
输出结果为:
09-20 06:24:00.890: I/System.out(937): 连接中....
09-20 06:24:01.030: I/System.out(937): 连接成功....
09-20 06:24:01.040: I/System.out(937): 版本2.0
这样就实现了自定义web服务