0

我正在尝试将小时 scheldule 旁边的粗体文本(来自http://golfnews.no/golfpaatv.php)放入字符串数组中,然后将其显示在我的设备屏幕上。我已经设置了 Internet 访问权限,所以这不是问题。应用程序在我的设备上崩溃了。原因:索引越界。我不明白问题出在哪里。我的代码是:

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.indexOf("<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;
    }


}

<h2>和的数量</h2>小于 10 。我的应用程序在第二次迭代时崩溃。我是android初学者,所以不太了解。你能给我一个提示吗?我真的很感激 。谢谢 !

PS:我知道 Index Out of bounds 是什么意思,我只是不知道为什么我在这里得到错误。

4

2 回答 2

2

IndexOutOfBoundsException当您想要访问超出范围的数组索引时,您会得到。例如:

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

会产生这样的异常。检查堆栈跟踪以查找此异常源自的行。它告诉你这个问题来自的类名/方法名/行号。

在您的情况下,我怀疑 中有超过 10 个<h2>trax因此您会遇到此异常。

最初您不知道<h2>' 的数量,因此更改此行:

String result[] = new String[10];

有了这个:

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

然后,您可以使用以下内容将元素添加到此列表中:

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

EDIT1
我也认为你的意思是:

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

编辑2

我还意识到您分配数组错误,您分配的是 10 个字符的字符串而不是 10 个字符串的数组:

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

//String result[] = new String[10];
String[] result = new String[10];
于 2011-09-26T10:32:45.460 回答
0

也试试这个。。

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

            }
于 2011-09-26T10:41:11.393 回答