0

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)
5
  • Whats the line number arrays.java:1731) Commented Jun 23, 2013 at 11:21
  • 1
    post your complete crash log Commented Jun 23, 2013 at 11:21
  • @Plato I have update the question with the crash log Commented Jun 23, 2013 at 11:29
  • 1
    get rid of the while loop and replace it with String buffer = EntityUtils.toString(responseEntity); Commented Jun 23, 2013 at 11:33
  • @blackbelt thanks after replacing the loop by your code its works now :P thanks Commented Jun 23, 2013 at 11:36

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.