的StringIndexOutOfBoundsException

时间:2014-05-02 16:59:33

标签: java android

我正在开发一个浏览器应用程序,我尝试从互联网上下载内容,但应用程序创建一个对话框,说“StringIndexOutOfBoundsException lenght = 79; regionStart = 55; regionLength = -56”

请帮帮我!

@Override
protected Long doInBackground(URL... url) {
    String urlDescarga = url[0].toString();
    mensaje = "";
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(urlDescarga);
    InputStream inputStream = null;
    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        BufferedHttpEntity bufferedHttpEntity =
                new BufferedHttpEntity(httpResponse.getEntity());
        inputStream = bufferedHttpEntity.getContent();
        String fileName = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/descargas";
        File directorio = new File(fileName);
        directorio.mkdirs();
        File file = new File(directorio, urlDescarga.substring(urlDescarga.lastIndexOf("/"), urlDescarga.indexOf("?")));
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while (inputStream.available() > 0 &&
                (len = inputStream.read(buffer)) != -1) {
            byteArray.write(buffer, 0, len);
        }
        fileOutputStream.write(byteArray.toByteArray());
        fileOutputStream.flush();
        mensaje = "Guardado en: " + file.getAbsolutePath();
    } catch (Exception ex) {
        mensaje = ex.getClass().getSimpleName() + " " + ex.getMessage();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
    return (long) 0;
}
protected void onPostExecute(Long result) {
    AlertDialog.Builder builder = new
            AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Descarga");
    builder.setMessage(mensaje);
    builder.setCancelable(true);
    builder.create().show();
} 

我把它放在onCreate:

    navegador.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(final String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {
            AlertDialog.Builder builder = 
                    new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Descarga");
            builder.setMessage("¿Desea guardar el archivo?");
            builder.setCancelable(false).setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    URL urlDescarga;
                    try {
                        urlDescarga = new URL(url);
                        new DescargarFichero().execute(urlDescarga);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }
                }
            }).setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            builder.create().show();
        }
    });

2 个答案:

答案 0 :(得分:1)

尝试

File file = null;
if(urlDescarga.indexOf("?") != -1) {
    file = new File(directorio, urlDescarga.substring(urlDescarga.lastIndexOf("/"), urlDescarga.indexOf("?")));     
} else {
    file = new File(directorio, urlDescarga.substring(urlDescarga.lastIndexOf("/"), urlDescarga.length()));
}

网址没有强制要求'?'。只有我们有参数

才会存在

答案 1 :(得分:0)

您的问题很可能是-1作为indexof调用的返回值。作为标准调试技术,您应该获取复杂语句之外的值,以便测试值。

int index1 = urlDescarga.lastIndexOf("/");
int index2 = urlDescarga.indexOf("?");
File file = null;

if ( index1 >= 0 && index2 >=0 )
    file = new File(directorio, urlDescarga.substring(index1, index2));'