【问题标题】:unable to fetch apk file from https url while running smoothly with http url使用 http url 顺利运行时无法从 https url 获取 apk 文件
【发布时间】:2016-08-11 06:30:27
【问题描述】:

我想通过本地服务器更新我的应用程序。我已将我的应用程序放在服务器上,并通过此代码执行更新它的任务

 public class UpdateClass extends AsyncTask<String, String, String> {


    ProgressDialog progressDialog;
    int status = 0;

    private Context context;

    public void onPreExecute() {
        progressDialog = new ProgressDialog(Update.this);
        progressDialog.setMessage("Please Wait.......");
        progressDialog.show();

    }

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

         //   String PATH = Environment.getExternalStorageDirectory() + "/Download/";
            String dir = Environment.getExternalStorageDirectory().getAbsolutePath() +  "/Download/";

            System.out.println("path..........." + Environment.getExternalStorageDirectory());
            File file = new File(dir);
            file.mkdirs();
            File outputFile = new File(file, "location.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();

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +
                    "/Download/" + "location.apk")), "application/vnd.android.package-archive");
            System.out.println("path........dsffffffffffffffsdfffff..." + Environment.getExternalStorageDirectory());

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);


        } catch (FileNotFoundException fnfe) {
            status = 1;
    //               Toast.makeText(context, "App not Available",   Toast.LENGTH_LONG).show();
            Log.e("File", "FileNotFoundException! " + fnfe);

        } catch (Exception e) {
            //   Toast.makeText(context, "App update", Toast.LENGTH_LONG).show();
            Log.e("UpdateAPP", "Exception " + e);
        }
        return null;
    }


    public void onPostExecute(String unused) {
        progressDialog.dismiss();
      /*  if (status == 1)
            Toast.makeText(context, "App not Available",        Toast.LENGTH_LONG).show();
    }*/
    }
}

代码在“http://localhost/androidservices/location.apk”下运行良好 但是当我用我的服务器地址 https://liveserver/androidservices/location.apk 替换这个 url 时,我在我的 logcat 和手机中得到文件未找到异常,下载了一个具有相同文件名的文件,大小为 0 kb。我可以通过浏览器访问 url,因为使用浏览器(chrome、mozilla)打开文件时很容易下载文件。

【问题讨论】:

    标签: android web-services iis


    【解决方案1】:

    我找到了这个查询的解决方案。 IIS 服务器不支持 post 方法,并且这个 url 使用的方法是 post,因为我使用过

      c.setRequestMethod("GET");
            c.setDoOutput(true);
    

    虽然我是通过GET,但使用的方法是post。我把它改成了

                 URL url = new URL(arg0[0]);
    
                HttpURLConnection c = (HttpURLConnectionurl.openConnection();
                c.connect();
    

    并删除了setDoOutput(true),因此方法更改为GET,程序开始顺利运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多