Java.lang.arrayindexoutofboundexception [英] Java.lang.arrayindexoutofboundexception

查看:1024
本文介绍了Java.lang.arrayindexoutofboundexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下功能适用于一些farmsid但是当我超过约检索数据。 30行我正在

  java.lang.arrayindexoutofboundexception:长度= 15802; regionstart = 1; regionlength = 15803在java.util.arrays.checkoffsetandcount(arrays.java:1731)

code

 公共静态的ArrayList<外勤及GT; GetFarmFields(UUID FarmId)
{
    ArrayList的<外勤及GT; FieldsList =新的ArrayList<外勤及GT;();    InputStream的流= NULL;    尝试
    {
        字符串URL = Params.GetServiceUrl()+/ GetFarmFields FarmId =?+ URLEn coder.en code(FarmId.toString())+&放大器; apikey =+ Params.GetApiKey();        HTTPGET请求=新HTTPGET(URL);
        request.setHeader(接受,应用/ JSON);
        request.setHeader(内容类型,应用/ JSON);        DefaultHttpClient的HttpClient =新DefaultHttpClient();
        HTT presponse响应= httpclient.execute(请求);
        HttpEntity responseEntity = response.getEntity();        INT CONTENTLENGTH =(int)的responseEntity.getContentLength();
        的char []缓冲区=新的char [CONTENTLENGTH]
        流= responseEntity.getContent();
        InputStreamReader的读者=新的InputStreamReader(流);        INT hasRead = 0;
        而(hasRead< CONTENTLENGTH)
            hasRead + = reader.read(缓冲,hasRead,CONTENTLENGTH-hasRead);        stream.close();        JSONArray JSFields =新JSONArray(新的String(缓冲));        的for(int i = 0; I< JSFields.length();我++)
        {
            JSONObject的JSField = JSFields.getJSONObject(I)            现场ThisField =新领域();            ThisField.setFieldId(UUID.fromString(JSField.getString(FieldId)));
            ThisField.setName(JSField.getString(姓名));
            ThisField.setDescription(JSField.getString(描述));
            ThisField.setSizeHA(Float.valueOf(JSField.getString(SizeHA)));
            ThisField.setSizeA(Float.valueOf(JSField.getString大小(sizeA)));
            ThisField.setOwner(JSField.getString(所有者));
            ThisField.setAnnualRent(JSField.getDouble(AnnualRent));
            ThisField.setLatitude(JSField.getDouble(纵横));
            ThisField.setLongitude(JSField.getDouble(经度));
            ThisField.setFarmId(UUID.fromString(JSField.getString(FarmId)));
            ThisField.setIsActive(JSField.getBoolean(IsActive));
            ThisField.setDeactivati​​onDate(Utility.JsonDateToDate(JSField.getString(Deactivati​​onDate)));
            ThisField.setAreas(JSField.getString(区域));
            ThisField.setUsageCount(JSField.getInt(UsageCount));
            ThisField.setLatestCulture(JSField.getString(LatestCulture));
            ThisField.setLatestSort(JSField.getString(LatestSort));            FieldsList.add(ThisField);
        }        Log.d(GetFarmFields,GetFarmFields结果:+ response.getStatusLine()的getStatus code()++新的String(缓冲)。);
    }
    赶上(JSONException E)
    {
        e.printStackTrace();
    }
    赶上(UnsupportedEncodingException五)
    {
        e.printStackTrace();
    }
    赶上(ClientProtocolException E)
    {
        e.printStackTrace();
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }
    最后
    {
        如果(流!= NULL)
        {
            尝试{stream.close();}赶上(IOException异常五){e.printStackTrace();}
        }
    }    返回FieldsList;
}

下面是我对异步任务

获取崩溃日志

//崩溃日志

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


解决方案

改变

  INT CONTENTLENGTH =(int)的responseEntity.getContentLength();
的char []缓冲区=新的char [CONTENTLENGTH]
流= responseEntity.getContent();
InputStreamReader的读者=新的InputStreamReader(流);INT hasRead = 0;
而(hasRead< CONTENTLENGTH)
      hasRead + = reader.read(缓冲,hasRead,CONTENTLENGTH-hasRead);stream.close();

 字符串缓冲区= EntityUtils.toString(responseEntity);

below function works for some farmsid but when I retrieve data with more than approx. 30 rows I am getting

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

Code

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;
}

below is the crash log which I am getting on the async task

//Crash log

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)

解决方案

change

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();

with

String buffer = EntityUtils.toString(responseEntity);

这篇关于Java.lang.arrayindexoutofboundexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆