我正在尝试将小时 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 是什么意思,我只是不知道为什么我在这里得到错误。