无法从Android中的远程服务器下载.apk文件

时间:2014-01-13 09:01:53

标签: java android

我想从我的网络服务器下载.apk文件,但我一直都会java.io.FileNotFoundException: http://xxx/mygames/myapks/demo.apk

但该文件存在于此URL中 下面是我的代码,我在做什么。

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

public class InstallAPK extends AsyncTask<String,Void,Void> {

    private Context context;
    public void setContext(Context contextf){
        context = contextf;
    }

    @Override
    protected Void doInBackground(String... arg0) {
        try {
            URL url = new URL(arg0[0]);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            File sdcard = Environment.getExternalStorageDirectory();
            File myDir = new File(sdcard,"Android/data/com.sush19.app/temp");
            myDir.mkdirs();
            File outputFile = new File(myDir, "temp.apk");
            if(outputFile.exists()){
                outputFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.flush();
            fos.close();
            is.close();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(sdcard,"Android/data/com.sush19.app/temp/temp.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
            context.startActivity(intent);


        } catch (Exception e) {
            Log.e("UpdateAPP", "Update error! " + e.getMessage());
        }
        return null;
    }
}

On button Click I'm calling as follow

InstallAPK downloadAndInstall = new InstallAPK();
downloadAndInstall.setContext(getApplicationContext());
downloadAndInstall.execute("http://xxx/mygames/myapks/demo.apk");

我也尝试使用文本文件,因为我刚刚将demo.txt文件上传到http://xxx/mygames/myapks/demo.txt我得到了同样的异常,但当我浏览这个demo.txt文件时,它会在浏览器中打开。从Web服务器下载.apk文件需要做什么
我还包括了所有必需的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我在上面得到了以上例外:

InputStream is = c.getInputStream();

2 个答案:

答案 0 :(得分:0)

我认为你的android sdk版本是4.x,也许你可以尝试删除行代码c.setDoOutput(true)。

答案 1 :(得分:0)

这不是你的代码错误。 这是服务器故障。只是尝试从浏览器中点击URL地址。