使用HttURLConnection显示数据库中的数据

时间:2019-01-16 00:36:36

标签: java android httpurlconnection

我正在显示数据库中的数据,但是不起作用。 那就是给我的异常:FileNotFound但路径很好。 但是当我从浏览器中输入URL时,它可以正常工作

有我的代码:

  @Override
    protected String doInBackground(String... parametro)
    {
        try
        {
            URL url  = new URL(urlMostrarClientes);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();

            InputStream inputStream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer buffer = new StringBuffer();
            String linha = "";

            while ((linha = bufferedReader.readLine()) != null)
            {
                buffer.append(linha + "\n");
            }

            inputStream.close();
            bufferedReader.close();
            progressDialog.dismiss();

            return buffer.toString().trim();

        } catch (MalformedURLException e)
        {
            e.printStackTrace();
            progressDialog.dismiss();
            Log.v("vampiro","ERRO : "  + e.toString());

        } catch (ProtocolException e)
        {
            e.printStackTrace();

            progressDialog.dismiss();
            Log.v("vampiro","ERRO : "  + e.toString());

        } catch (IOException e) {
            e.printStackTrace();
            progressDialog.dismiss();
            Log.v("vampiro","ERRO : "  + e.toString());
        }

        return "ERRO";
    }

1 个答案:

答案 0 :(得分:0)

解决问题的三种方法:

  1. file:/// yourFilePath
  2. Paths.get(yourPath).toUri()。toURL()// java nio方法
  3. File(“ path_to_file”)。toURI()。toURL();  // java io方式