简体   繁体   English

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

[英]Unable to download .apk file from remote server in Android

I want to download .apk file from my web server, but I'm getting 我想从网络服务器上下载.apk文件,但是
java.io.FileNotFoundException: http://xxx/mygames/myapks/demo.apk all the time. java.io.FileNotFoundException: http://xxx/mygames/myapks/demo.apk

But the file is present in this URL Below is my code, What I'm doing. 但是该文件存在于该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");

I tried with text file as well, for that I just uploaded demo.txt file to http://xxx/mygames/myapks/demo.txt I'm getting same Exception, but when I browse this demo.txt file URL it opens in the Browser. 我也尝试使用文本文件,因为我只是将demo.txt文件上传到http://xxx/mygames/myapks/demo.txt我也遇到同样的异常,但是当我浏览此demo.txt文件URL时,它会打开在浏览器中。 What I need to do to download .apk file from the Web server. 我需要从Web服务器下载.apk文件的步骤。

I've also Included all the required permissions 我还包括了所有必需的权限

<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" />

I'm getting the above Exception at: 我在上面得到上面的异常:

InputStream is = c.getInputStream();

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

It is not your code fault. 这不是您的代码错误。 It is server fault. 是服务器故障。 just try to hit URL address from Browser. 只需尝试从浏览器中访问URL地址即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM