开发者

Android Index out of bounds

I am trying to get the bold text next to the hour scheldule ( from http://golfnews.no/golfpaatv.php )put it in a String array , then , show it on my device's screen. I've put the Internet Access permission , so that is not the problem.The application crashes on my device . Reason : index out of bounds . I don't understand where's the problem . My code is :

package com.work.webrequest;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class WebRequest extends Activity {


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String trax;

        String aux[] = new String[10];

        setContentView(R.layout.main);
        TextView txt = (TextView) findViewById(R.id.textView1);
        trax=getPage();

       aux= title (trax);


       txt.setText(" Here I will display every title!!!");

    }
    private String[] title (String trax)
    {

        String result[] = new String[10];
        int ok=1;
        int s1,s2;
        int i=0;
        while(ok==1)
        {
            System.out.println("INDEX = "+trax.indexOf("<h2>"));
            ok=0;
            if(trax.indexOf("<h2>")!=-1)
            {


            ok=1;
            s1 =trax.inde开发者_运维知识库xOf("<h2>");
            s2 = trax.indexOf("</h2>");
            result[i] = trax.substring(s1+4,s2);
            i++;
            trax = trax.substring(trax.indexOf("</h2>"));

            }

        }
         return result;
    }

    private String getPage() {
        String str = "***";

        try
        {
            HttpClient hc = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://www.golfnews.no/feed.php?feed=1");
            HttpResponse rp = hc.execute(post);

            if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(rp.getEntity());
            }
        }catch(IOException e){
            e.printStackTrace();
        }  

        return str;
    }


}

The number of <h2> and </h2> is below 10 . My application crashes at the 2nd iteration . I am a beginner in android , so I don't know much . Could you give me a hint ? I would really appreciate it . Thank you !

PS : I know what Index Out of bounds means , I just don't know why I get the error here .


You get IndexOutOfBoundsException when you want to access an array index which is out of range. For example:

String[] myArray = new String[10];
myArray[10] = "test"; // 10 is out of limits(0-9)

Would produce such an exception. Check the stacktrace for the line that this exception originates from. It tells you the class name/method name/line number that this problem comes from.

In your case I suspect there are more than 10 <h2> in the trax, so you get this exception.

Initially you don't know the number of <h2>'s so change this line:

String result[] = new String[10];

with this:

ArrayList<String> result= new ArrayList<String>(); 

Then you can add elements to this list with the following:

// result[i] = trax.substring(s1+4,s2);
result.add(trax.substring(s1+4,s2));

EDIT1
Also I think you mean this:

//trax = trax.substring(trax.indexOf("</h2>"));
trax = trax.substring(s2 + 5);

EDIT2

Also I realized you allocate arrays wrong, you are allocating a String of 10 chars instead of an array of 10 Strings:

//String aux[] = new String[10];
String[] aux = new String[10];

//String result[] = new String[10];
String[] result = new String[10];


try this one too..

if(trax.indexOf("<h2>")!=-1&&i<10)
            {


            ok=1;
            s1 =trax.indexOf("<h2>");
            s2 = trax.indexOf("</h2>");
            result[i] = trax.substring(s1+4,s2);
            i++;
            trax = trax.substring(trax.indexOf("</h2>"));

            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜