2012-07-21 11 views
6

Dopo aver svolto alcune esercitazioni di base, ho iniziato a creare la mia prima vera app per Android in Eclipse. Voglio che questa app verifichi se il testo in un EditText corrisponde al testo su un sito web (questo: http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf (contiene le modifiche alla pianificazione della mia scuola)). Ho scoperto come fare in modo che l'app verifichi se il testo in EditText corrisponde a una stringa (con il metodo contains()), quindi ora l'unica cosa che devo fare è scaricare tutto il testo di quel sito Web in una stringa. Ma non ho idea di come. O forse c'è un metodo che posso verificare se un sito web contiene una determinata parola senza scaricare il testo del sito web in una stringa.Download di un sito Web in una stringa

Grazie!

(. BTW io non sono inglese in modo plz mi perdoni se ho fatto qualche errore relative alla lingua)

@androider non posso postare il mio codice nella casella di commento ecco che è:

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(); 
     } 
    } 
    } 
}   

Qui ci sono le proprietà del pulsante:

<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" > 

risposta

5

È possibile ottenere il testo utilizzando InputStream Reader come questo.

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) { 
} 

aggiungere anche un permesso aggiuntivo nelle vostre applicazioni di file manifesto:

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

// ====================== ====== EDIT ================================ //

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

Grazie per la tua risposta, ma non sembra funzionare correttamente, cosa sto facendo male: – Xander

+0

Ok, sono nuovo qui, come posso pubblicare il mio codice? :) – Xander

+0

Puoi postarlo facendo clic sull'opzione Modifica..e incollare lì il tuo codice .. –

1

Questa è una buona domanda correlata Java: How do you Programmatically Download a Webpage in Java

Puoi quindi implementare qualsiasi metodo tu scelga nella tua app. Una cosa da notare è che dovrai abilitare l'autorizzazione INTERNET sulla tua app per accedere a Internet.

Problemi correlati