Java StringIndexOutOfBoundsException
我正在开发一个浏览器应用程序,我将此应用程序用于尝试从internet下载内容,但该应用程序会创建一个对话框,上面写着“StringIndexOutOfBoundsException Length=79;regionStart=55;regionLength=-56” 请帮帮我Java StringIndexOutOfBoundsException,java,android,Java,Android,我正在开发一个浏览器应用程序,我将此应用程序用于尝试从internet下载内容,但该应用程序会创建一个对话框,上面写着“StringIndexOutOfBoundsException Length=79;regionStart=55;regionLength=-56” 请帮帮我 @Override protected Long doInBackground(URL... url) { String urlDescarga = url[0].toString(); mensaje
@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();
}
});
试一试
url不必强制使用“?”。只有当我们有参数时,它才会出现。您的问题很可能是indexof调用的返回值为-1。作为一种标准的调试技术,您应该获取复杂语句之外的值,以便测试这些值
int index1 = urlDescarga.lastIndexOf("/");
int index2 = urlDescarga.indexOf("?");
File file = null;
if ( index1 >= 0 && index2 >=0 )
file = new File(directorio, urlDescarga.substring(index1, index2));'
(
urlDescarga.indexOf(“?”)
返回-1,很明显)堆栈跟踪位于LogCat
中,现在该对话框不显示任何内容,但不显示下载。这可能是因为url没有“?”。我的答案是正确的。试试那样的。试着打印出url,这样你就可以看到如何准确地进行拆分。
int index1 = urlDescarga.lastIndexOf("/");
int index2 = urlDescarga.indexOf("?");
File file = null;
if ( index1 >= 0 && index2 >=0 )
file = new File(directorio, urlDescarga.substring(index1, index2));'