简体   繁体   English

无法与HTTP网址顺利运行时,无法从https网址获取apk文件

[英]unable to fetch apk file from https url while running smoothly with http url

I want to update my app through local server. 我想通过本地服务器更新我的应用程序。 I have placed my application on server and through this code i am doing the task of updating it 我已将我的应用程序放置在服务器上,并通过此代码执行更新它的任务

 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();
    }*/
    }
}

the code works fine with " http://localhost/androidservices/location.apk " but when i replace this url with my server address that is https://liveserver/androidservices/location.apk then i get file not found exception in my logcat and in mobile phone a file with same file name is downloaded which size is 0 kb. 该代码可以与“ http://localhost/androidservices/location.apk ”正常工作,但是当我用我的服务器地址https://liveserver/androidservices/location.apk替换此url时,在我的文件中找不到文件异常logcat,并在手机中下载了具有相同文件名的文件,大小为0 kb。 i can access the url through browser as the file is easily being downloaded when opened with browser(chrome,mozilla). 我可以通过浏览器访问该网址,因为使用浏览器(chrome,mozilla)打开该文件时很容易下载。

I found the solution for this query. 我找到了此查询的解决方案。 IIS servers don't support post methods and the method this url was using was post since i had used IIS服务器不支持发布方法,并且该URL使用的方法是发布,因为我曾经使用过

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

Although i was passing GET but the method used was post.I changed it to 虽然我通过了GET,但是使用的方法是post。我将其更改为

             URL url = new URL(arg0[0]);

            HttpURLConnection c = (HttpURLConnectionurl.openConnection();
            c.connect();

and removed setDoOutput(true) and thus the method changed to GET and program started to work smoothly. 并删除了setDoOutput(true),因此方法更改为GET,程序开始正常运行。

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

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