時間のスケジュールの横にある太字のテキストを取得しようとしています ( http://golfnews.no/golfpaatv.phpから) 文字列配列に入れてから、デバイスの画面に表示します。インターネット アクセス許可を設定したので、それは問題ではありません。デバイスでアプリケーションがクラッシュします。理由: 索引が範囲外です。どこに問題があるのかわからない。私のコードは次のとおりです。
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 未満です。アプリケーションが 2 回目の繰り返しでクラッシュします。私はアンドロイドの初心者なので、よくわかりません。ヒントを教えてください。とても感謝しております 。ありがとうございました !
PS: Index Out of bounds の意味は知っていますが、ここでエラーが発生する理由がわかりません。