提取apk文件 (四) 大结局

前三篇

先上成果。

手机=》web服务=》什么服务=》提供下载手机里的apk文件

=》干什么用=》从市场下载应用供apk上传到电脑的问题=》反编译它用来学习。

 

ok,先回到应用本身。

上篇我们完成了一个简单的web服务器。

然后我们在Activity里用一个线程启动服务器。注意在权限里增加internet权限。因为这个耽误了半个小时。呜呜。

// TODO 使用一个线程来启动服务器
new Thread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
new HttpServer().start(8000);
Log.v(tag, "http server start");

}
}).start();


然后根据需要提供列出所有应用,以及文件下载的两个http Handler。

public class DefaultHandler implements IHandler{
public String makeList(){
String body="";
List<ResolveInfo> apps = ApkActivity.apps;
PackageManager pm = ApkActivity.pm;
String path;
if(apps!=null&&apps.size()>0){
for(ResolveInfo app:apps){
path=app.activityInfo.applicationInfo.sourceDir;
body+="<p><a href='"+path+"'>"+app.loadLabel(pm)+"</a></p>";
}
}
return body;

}
public void doResponse(DataOutputStream out) throws IOException{
out.writeBytes("HTTP/1.0 200 \r\n");
out.writeBytes("Content-Type: text/html \r\n");
out.writeBytes("\r\n");
String head="<meta http-equiv=Content-Type content=text/html; charset=UTF-8>";
out.writeBytes(head);
out.writeBytes("<h1>Your Apps</h1>");
out.write(makeList().getBytes());
out.flush();
}
}

默认的首页,相当于index。列出所以app。这里的代码质量不太好。先这样,出个alpha版本再说。

public class FileHandler implements IHandler{
private File file;
public FileHandler(File f) {
file=f;
// TODO Auto-generated constructor stub
}
public void doResponse(DataOutputStream out) throws IOException{
if(file.exists()){
out.writeBytes("HTTP/1.0 200 OK\r\n");
out.writeBytes("Content-Type: application/octet-stream \r\n");
out.writeBytes("\r\n");
FileInputStream fis = new FileInputStream(file);
byte buf[]=new byte[1024];
int r;
while((r=fis.read(buf))!=-1){
out.write(buf);
}
}else{
out.writeBytes("HTTP/1.0 404 File Not Found\r\n");
out.writeBytes("\r\n");
}
out.flush();
}
}

文件下载的handler。就是根据文件的路径下载咯。linux文件系统没有盘符的考虑,这点处理起来比较方便。

    private void doResponse() {
// TODO Auto-generated method stub
//先做简单的处理
if(query!=null){
try {
OutputStream out = waiter.getOutputStream();
DataOutputStream dos=new DataOutputStream(out);
if(query.equals("/")){
new DefaultHandler().doResponse(dos);
}else if(query.endsWith(".apk")){
new FileHandler(new File(query)).doResponse(dos);
}
waiter.shutdownOutput();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

经过改造的doResponse方法。本来想实现urlmap,不过没经验,先这样将就吧。哈哈。

 

到此为止,0.1版本的,嗯,想个名字,apk getter,就完成了。

 

 

 

@我是函数,出品,版权所有,转载请注明出处。

需要代码的请留下邮箱,代码虽烂,情谊在。

欢迎交流。

 



 》》》注意

文件输出用DataOutputStream存在错误,已经改用原始的outputstream。



 

转载于:https://www.cnblogs.com/jcoder/archive/2012/03/18/2404929.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值