gpt4 book ai didi

java.lang.arrayindexoutofbound异常

转载 作者:行者123 更新时间:2023-12-01 14:24:39 24 4
gpt4 key购买 nike

下面的函数适用于某些 farmsid,但是当我检索超过大约的数据时。我得到 30 行

 java.lang.arrayindexoutofboundexception:length=15802;regionstart=1;regionlength=15803 at java.util.arrays.checkoffsetandcount(arrays.java:1731)

代码

public static ArrayList<Field> GetFarmFields(UUID FarmId)
{
ArrayList<Field> FieldsList = new ArrayList<Field>();

InputStream stream = null;

try
{
String Url = Params.GetServiceUrl() + "/GetFarmFields?FarmId="+URLEncoder.encode(FarmId.toString())+"&apikey="+Params.GetApiKey();

HttpGet request = new HttpGet(Url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(request);
HttpEntity responseEntity = response.getEntity();

int contentLength = (int) responseEntity.getContentLength();
char[] buffer = new char[contentLength];
stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);

int hasRead = 0;
while (hasRead < contentLength)
hasRead += reader.read(buffer, hasRead, contentLength-hasRead);

stream.close();

JSONArray JSFields = new JSONArray(new String(buffer));

for(int i=0; i<JSFields.length(); i++)
{
JSONObject JSField = JSFields.getJSONObject(i);

Field ThisField = new Field();

ThisField.setFieldId(UUID.fromString(JSField.getString("FieldId")));
ThisField.setName(JSField.getString("Name"));
ThisField.setDescription(JSField.getString("Description"));
ThisField.setSizeHA(Float.valueOf(JSField.getString("SizeHA")));
ThisField.setSizeA(Float.valueOf(JSField.getString("SizeA")));
ThisField.setOwner(JSField.getString("Owner"));
ThisField.setAnnualRent(JSField.getDouble("AnnualRent"));
ThisField.setLatitude(JSField.getDouble("Latitude"));
ThisField.setLongitude(JSField.getDouble("Longitude"));
ThisField.setFarmId(UUID.fromString(JSField.getString("FarmId")));
ThisField.setIsActive(JSField.getBoolean("IsActive"));
ThisField.setDeactivationDate(Utility.JsonDateToDate(JSField.getString("DeactivationDate")));
ThisField.setAreas(JSField.getString("Areas"));
ThisField.setUsageCount(JSField.getInt("UsageCount"));
ThisField.setLatestCulture(JSField.getString("LatestCulture"));
ThisField.setLatestSort(JSField.getString("LatestSort"));

FieldsList.add(ThisField);
}

Log.d("GetFarmFields", "GetFarmFields Result: " + response.getStatusLine().getStatusCode()+" "+new String(buffer));
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (stream != null)
{
try {stream.close();} catch (IOException e) {e.printStackTrace();}
}
}

return FieldsList;
}

下面是我在异步任务中收到的崩溃日志

//崩溃日志

06-23 14:25:07.916: W/System.err(4254): java.lang.ArrayIndexOutOfBoundsException: length=15802; regionStart=-1; regionLength=15803
06-23 14:25:07.916: W/System.err(4254): at java.util.Arrays.checkOffsetAndCount(Arrays.java:1731)
06-23 14:25:07.916: W/System.err(4254): at java.io.InputStreamReader.read(InputStreamReader.java:218)
06-23 14:25:07.916: W/System.err(4254): at com.helpers.plantivomobilepro.FieldHelper.GetFarmFields(FieldHelper.java:93)
06-23 14:25:07.926: W/System.err(4254): at com.andreas.plantivomobilepro.FieldsActivity$5.doWork(FieldsActivity.java:245)
06-23 14:25:07.926: W/System.err(4254): at com.andreas.plantivomobilepro.FieldsActivity$5.doWork(FieldsActivity.java:1)
06-23 14:25:07.926: W/System.err(4254): at com.andreas.plantivomobilepro.CustomAsyncTask.doInBackground(CustomAsyncTask.java:25)
06-23 14:25:07.926: W/System.err(4254): at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-23 14:25:07.926: W/System.err(4254): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-23 14:25:07.926: W/System.err(4254): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-23 14:25:07.936: W/System.err(4254): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-23 14:25:07.936: W/System.err(4254): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-23 14:25:07.936: W/System.err(4254): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-23 14:25:07.936: W/System.err(4254): at java.lang.Thread.run(Thread.java:856)

最佳答案

改变

int contentLength = (int) responseEntity.getContentLength();
char[] buffer = new char[contentLength];
stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);

int hasRead = 0;
while (hasRead < contentLength)
hasRead += reader.read(buffer, hasRead, contentLength-hasRead);

stream.close();

String buffer = EntityUtils.toString(responseEntity);

关于java.lang.arrayindexoutofbound异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com