为什么以及我的数组被抛出界限?
public class DownloadRawData extends AsyncTask<String, Void, String>
{
@Override
public void onPostExecute(String webData) {
mData =webData;
Log.v(LOG_TAG, "Data returned was: "+mData);
if(mData ==null) {
if (mRawURL ==null) {
mDownloadStatus = DownloadStatus.NOT_INITIALIZED;
} else {
mDownloadStatus =DownloadStatus.FAILED_OR_EMPTY;
}
} else {
mDownloadStatus =DownloadStatus.OK;
}
}
protected String doInBackground(String...params){
HttpURLConnection urlConnection =null;
BufferedReader reader = null;
if(params ==null)
return null;
try{
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
if (inputStream ==null){
return null;
}
StringBuilder buffer= new StringBuilder();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line =reader.readLine()) !=null) {
buffer.append(line + "\n");
}
return buffer.toString();
} catch (IOException e) {
Log.e(LOG_TAG,"Error",e);
return null;
} finally {
if(urlConnection !=null) {
urlConnection.disconnect();
}
if(reader !=null) {
try {
reader.close();
}
catch (final IOException e) {
Log.e(LOG_TAG,"ERROR closing stream",e);
}
}
}
继承我的错误
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.google.android.gms.samples.vision.face.multitracker.GetRawData$DownloadRawData.doInBackground(GetRawData.java:84)
at com.google.android.gms.samples.vision.face.multitracker.GetRawData$DownloadRawData.doInBackground(GetRawData.java:55)
Java第55行是
public class DownloadRawData extends AsyncTask<String, Void, String>
Java第84行是
URL url = new URL(params[0]);
答案 0 :(得分:4)
此代码:
if(params ==null)
return null;
不会阻止调用者在没有任何参数的情况下调用您的方法,在这种情况下,您的params
将是一个空数组,因此大小为零,这就是您接收的原因例外。
您应该在该检查中添加params.length > 0
之类的内容,或者以其他方式更改方法的逻辑以处理这种情况。