您的位置:首页 > 理论基础 > 计算机网络

android 网络获取字符串

2016-05-19 10:10 507 查看
获取网络数据 &1.0&vedi&http://192.168.1.105:5236/vedio/APK.apk

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);
}


整个流程记录一下,对于下次项目使用做更好的使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: