0

私はかなり新しい Android アプリの開発で、私はいくつかの助けが必要です。ユーザーに単語の入力を求めるこの単純な辞書アプリケーションを作成し、ボタンを押すと、その単語がインターネット (おそらく wikipedia.org) に送られ、その情報がユーザーに返されます。XML を使用してアプリのテキスト フィールドとボタンを開発しました。そして、OnClickListener を使用して回答が何であれ設定されるテキスト (ansa) を作成しました。Web ビューを設定したくありません。テキストを辞書の回答に設定したいだけです。これが私がこれまでにできたことです。Googleからデータを取得するためのこのクラスがあります。

public class GetMethod {

public String getInternetData() throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI website = new URI("http://www.google.com");
        HttpGet request = new HttpGet();
        request.setURI(website);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String l = "";
        String nl = System.getProperty("line.separator");
        while((l = in.readLine()) !=null){
            sb.append(l + nl);
        }
        in.close();
        data = sb.toString();
        return data;
    }finally{
        if (in !=null){
            try{
                in.close();
                return data;
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

XMLが実装されている別のクラス

public class Dictionary extends Activity {

TextView ansa;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dictionary);

    Button Search = (Button) findViewById(R.id.search);

    ansa = (TextView) findViewById(R.id.ansa);   

    final GetMethod test = new GetMethod();





    Search.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
             String returned;
                try {
                    returned = test.getInternetData();
                    ansa.setText(returned);

                } catch (Exception e) {
                    ansa.setText("Failed");
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



        }   
    });






}

実行すると、Web サイト全体の HTML が取得されます

したがって、ユーザーの言葉を wiki Web サイトに取り込み、ansa のテキストのみを取得し、おそらく解析して文字列に保存する方法について助けが必要です。本当にありがとうございました。

4

1 に答える 1

2

Google Dictionarydictionary.comなどの API を使用できます。

ただし、HTTP クライアントを実装して応答を解析する必要があります。そして、目的のデータを表示します。

于 2012-07-25T20:15:38.673 に答える