如何从android下载授权网站下载文件

时间:2013-11-18 11:14:58

标签: android download authorization

下午好!

请帮我在下载文件时处理网站上的授权。我开发了一个可以从网站下载更新的应用程序。该网站未使用授权,但现在它已经出现。我添加了授权代码,但是它不起作用,文件被下载但没有像不正确的文件格式那样启动。

现在我只是android中的初学者,可能是我什么都不懂,丢失了。

这是我的代码模块。

public void checkUpdate(Context contextf, String urlAdress) {

    try {
        HttpURLConnection c = (HttpURLConnection) new URL(urlAdress).openConnection();
        c.setDoOutput(true);
        c.setUseCaches(false);

        // new strings in code for authorization \/
        c.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(("myUser:myPassword").getBytes(), Base64.NO_WRAP)); 
        //\

        c.connect();

        String PATH = "/mnt/sdcard/Download/";
        File file = new File(PATH);
        file.mkdirs();
        File outputFile = new File(file, "Update.apk");
        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.close();
        is.close();

        PackageInfo infoNewApp = context.getPackageManager().getPackageArchiveInfo(PATH + "Update.apk", PackageManager.GET_META_DATA);
        if (infoNewApp != null) {
            ApplicationInfo appInfo = infoNewApp.applicationInfo;
            if (Build.VERSION.SDK_INT >= 8) {

                appInfo.sourceDir = PATH;
                appInfo.publicSourceDir = PATH;

            }
            int vcodeNewApp = infoNewApp.versionCode;
            PackageInfo infoApp = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            if (infoApp != null) {

                int vcodeApp = infoApp.versionCode;

                if (vcodeNewApp > vcodeApp) {
                    LaunchUpdate();
                } else {
                }

            }
        } else {

        }

    } catch (Exception e) {
        Log.d(TAG, "Update error! " + e.getMessage());

    }

}

0 个答案:

没有答案