0

時間のスケジュールの横にある太字のテキストを取得しようとしています ( 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 の意味は知っていますが、ここでエラーが発生する理由がわかりません。

4

2 に答える 2

2

IndexOutOfBoundsException範囲外の配列インデックスにアクセスしたい場合に取得します。例えば:

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

そのような例外を生成します。この例外が発生した行のスタック トレースを確認します。この問題の原因となったクラス名/メソッド名/行番号が表示されます。

<h2>あなたの場合、に 10 個以上あると思われる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);

EDIT2

また、配列の割り当てが間違っていることに気付きました。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 に答える