码迷,mamicode.com
首页 > 其他好文 > 详细

版本更新之模拟数据下载更新

时间:2016-09-20 15:12:28      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 

在tomact服务器中自定义web服务,模拟数据下载更新

 

1.开启tomcat

  目录apache-tomcat-7.0.68\bin\startup.bat

2.将apache-tomcat-7.0.68\webapps目录下的WEB-INF拷贝到自定义的目录中,这样就可以访问自定义内容

如:拷贝到apache-tomcat-7.0.68\myApp目录,就可以在浏览器中访问该目录下的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服务

版本更新之模拟数据下载更新

标签:

原文地址:http://www.cnblogs.com/fangg/p/5888845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!