android 网络获取字符串
2016-05-19 10:10
507 查看
获取网络数据 &1.0&vedi&http://192.168.1.105:5236/vedio/APK.apk
实体类
获取网络访问的路径
GetServerUrl.getUrl()
在activity 执行注意线程问题
发送消息是要执行
下面执行文件下载 downFile(info.getUrl()) 执行这个方法
下面执行down()方法
整个流程记录一下,对于下次项目使用做更好的使用
public class UpdateInfoService { public UpdateInfoService(Context context) { } public UpdateInfo getUpDateInfo() throws Exception { String path = GetServerUrl.getUrl() + "/update.txt"; StringBuffer sb = new StringBuffer(); String line = null; BufferedReader reader = null; try { // 创建一个url对象0 URL url = new URL(path); // 通過url对象,创建一个HttpURLConnection对象(连接) HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); // 通过HttpURLConnection对象,得到InputStream reader = new BufferedReader(new InputStreamReader( urlConnection.getInputStream())); // 使用io流读取文件 while ((line = reader.readLine()) != null) { sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } String info = sb.toString(); UpdateInfo updateInfo = new UpdateInfo(); updateInfo.setVersion(info.split("&")[1]); updateInfo.setDescription(info.split("&")[2]); updateInfo.setUrl(info.split("&")[3]); //返回实体类 return updateInfo; } }
实体类
public class UpdateInfo { private String version; private String description; private String url; public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
获取网络访问的路径
GetServerUrl.getUrl()
public class GetServerUrl { public static String getUrl() { return application.getupdataurl; } }
在activity 执行注意线程问题
new Thread() { public void run() { try { UpdateInfoService updateInfoService = new UpdateInfoService( AdvertiselMainActivity.this); info = updateInfoService.getUpDateInfo(); // 当从服务器捞到的数据等于 “vedio” 更新视频 if(info.getDescription().equals("vedio")){ fileRW.deleteFile(); } Message message = new Message(); message.what = 0; handler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } }; }.start();
发送消息是要执行
public Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0: if (isNeedUpdate()) { Handler handle=new Handler(); handle.postDelayed(new Runnable() { @Override public void run() { downFile(info.getUrl()); } },1000*5); } break; } };
下面执行文件下载 downFile(info.getUrl()) 执行这个方法
void downFile(final String url) { pBar = new ProgressDialog(AdvertiselMainActivity.this); //进度条,在下载的时候实时更新进度,提高用户友好度 pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pBar.setTitle("正在下载"); pBar.setMessage("请稍候..."); pBar.setProgress(0); pBar.show(); new Thread() { public void run() { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response; try { response = client.execute(get); HttpEntity entity = response.getEntity(); int length = (int) entity.getContentLength(); //获取文件大小 pBar.setMax(length); //设置进度条的总长度 InputStream is = entity.getContent(); FileOutputStream fileOutputStream = null; if (is != null) { File file = new File( Environment.getExternalStorageDirectory(), "NICE.apk"); fileOutputStream = new FileOutputStream(file); byte[] buf = new byte[1024]; int ch = -1; int process = 0; while ((ch = is.read(buf)) != -1) { fileOutputStream.write(buf, 0, ch); process += ch; pBar.setProgress(process); } } fileOutputStream.flush(); if (fileOutputStream != null) { fileOutputStream.close(); } down(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); }
下面执行down()方法
void down() { handler.post(new Runnable() { public void run() { pBar.cancel(); update(); } }); } void update() { Toast.makeText(AdvertiselMainActivity.this, "下载之后更新", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "NICE.apk")), "application/vnd.android.package-archive"); startActivity(intent); }
整个流程记录一下,对于下次项目使用做更好的使用
相关文章推荐
- HttpURLConnection
- 基于AFNetworking3.0网络封装
- iOS获取本地视频和网络URL视频的缩略图方法
- 网络工程师需要掌握的10个有用的Perl模块
- Node安装及搭建简单HTTP服务器
- android 23以上使用httpclient
- 理解SSL必须要理解的密码技术
- cisco ASA 防火墙安全算法原理和基本配置(转自http://xiaoxiao1001.blog.51cto.com/7640562/1300161)
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误
- HTTP状态码(HTTP Status codes)简介
- 网络举报:公民的维权之路到底有多难?
- linux下的tcp连接超时
- windows7系统怎么打开指定端口 windows7检查端口未打开还是网络故障方法
- strace命令 来自: http://man.linuxde.net/strace
- 使用思科Packet tracer软件模拟PPP协议
- service
- 1分钟快速生成用于网页内容提取的xslt
- 从自我学习到深层网络
- centos7 无法连接无线网络 更新内核版本
- TCP协议中的三次握手和四次挥手(图解)