2012-07-21 43 views
6

完成了一些基礎教程後,我開始在eclipse中製作第一個真正的android應用程序。我想要這個應用程序來檢查EditText中的文本是否與網站上的文本匹配(這一個是:http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf(它包含我學校的日程安排更改))。我發現如何讓應用程序檢查EditText中的文本是否匹配字符串(方法contains()),所以現在我唯一需要做的就是將該網站的所有文本下載到串。但我不知道如何。或者是否有一種方法可以檢查一個網站是否包含某個詞,而不會將網站的文本下載到字符串中。將網站下載到字符串

謝謝!

(順便說一句,我不是英語,所以PLZ原諒我,如果我做了一些語言相關的錯誤)

@androider我不能在評論欄中發表我的代碼,所以在這裏,它是:

package me.moop.mytwitter; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.MalformedURLException; 
import java.net.URL; 

import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 
import android.app.ProgressDialog; 

public class MainActivity extends Activity { 

Button mBtnCheck; 
EditText mEtxtGroup; 
ProgressDialog mProgressDialog; 
TwitterUser mTwitterUser; 
TextView mTxtv1; 

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

    mBtnCheck = (Button) findViewById(R.id.btnCheck); 
    mEtxtGroup = (EditText) findViewById(R.id.etxtGroup); 
    mTxtv1 = (TextView) findViewById(R.id.textView1); 

} 

    public void checkScheduleChange(View view){ 
    if (view == mBtnCheck){ 
     String group; 
     group = mEtxtGroup.getText().toString(); 
     if (group.length() > 0){ 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Bezig met checken voor roosterwijzigingen..."); 
      mProgressDialog.show(); 
      try 
      { 
       URL url = new URL("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf"); 
       BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
       String str; 
       while ((str = in.readLine()) != null){ 
        mProgressDialog.dismiss(); 
        if(str.contains(mEtxtGroup.getText().toString())){ 
         Toast.makeText(this, "U hebt een roosterwijziging.", Toast.LENGTH_LONG).show(); 
        } 
        else{ 
         Toast.makeText(this, "U hebt geen roosterwijzigingen", Toast.LENGTH_LONG).show(); 
        } 
       } 
       in.close(); 
      } catch (MalformedURLException e) { 
       Toast.makeText(this, "Er is een fout opgetreden, probeer opniew.", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(this, "Er is een fout opgetreden, probeer opniew.", Toast.LENGTH_LONG).show(); 
      } 
     } 
     else{ 
      Toast.makeText(this, "Voer een klas in", Toast.LENGTH_LONG).show(); 
     } 
    } 
    } 
}   

這裏是按鈕的屬性:

<Button 
     android:id="@+id/btnCheck" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:clickable="true" 
     android:onClick="checkScheduleChange" 
     android:text="Check" > 

回答

5

你可以像這樣使用InputStream Reader來獲取文本。

try 
{ 
    URL url = new URL("http://yourwebpage.com"); 
    // Read all the text returned by the server 
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
    String str; 
    while ((str = in.readLine()) != null) 
    { 
    // str is one line of text; readLine() strips the newline character(s) 
    // You can use the contain method here. 
     if(str.contains(editText.getText().toString)) 
     { 
      You can perform your logic here!!!!!   
     } 
    } 
    in.close(); 
} catch (MalformedURLException e) { 
} catch (IOException e) { 
} 

此外,在您的應用程序添加一個額外的許可清單文件:

<uses-permission android:name="android.permission.INTERNET/> 

// ====================== ======編輯================================ //

if (group.length() > 0) 
    { 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Bezig met checken voor roosterwijzigingen..."); 
      mProgressDialog.show(); 
      try 
      { 
       URL url = new URL("http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf"); 
       BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
       String str; 
       while ((str = in.readLine()) != null){ 

        if(str.contains(mEtxtGroup.getText().toString())){ 

        if(mProgressDialog.isShowing()) 
        { 
         mProgressDialog.dismiss(); 
        } 


        Toast.makeText(this, "U hebt een roosterwijziging.", Toast.LENGTH_LONG).show(); 
        break; 
        } 
       } 
       in.close(); 
      } catch (MalformedURLException e) { 
       Toast.makeText(this, "Er is een fout opgetreden, probeer opniew.", Toast.LENGTH_LONG).show(); 
      } catch (IOException e) { 
       Toast.makeText(this, "Er is een fout opgetreden, probeer opniew.", Toast.LENGTH_LONG).show(); 
      } 

        if(mProgressDialog.isShowing()) 
        { 
         mProgressDialog.dismiss(); 
        } 
     } 
     else{ 
      Toast.makeText(this, "Voer een klas in", Toast.LENGTH_LONG).show(); 
     } 
+0

謝謝爲你的答覆,但它似乎沒有正常工作,我做錯了什麼: – Xander 2012-07-21 10:07:25

+0

好吧,我在這裏是新的,我怎麼張貼我的代碼? :) – Xander 2012-07-21 10:14:48

+0

您可以通過單擊編輯選項來發布它,然後粘貼您的代碼。 – 2012-07-21 10:16:03