0

下面的函数适用于一些农场,但是当我检索超过大约的数据时。我得到 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)
4

1 回答 1

1

改变

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);
于 2013-06-23T11:37:25.003 回答