2012-07-21 19 views
6

Después de haber hecho algunos tutoriales básicos, comencé a hacer mi primera aplicación de android real en eclipse. Quiero que esta aplicación compruebe si el texto de un EditText coincide con el texto de un sitio web (este: http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf (contiene los cambios de horario de mi escuela)). Descubrí cómo hacer que la aplicación revise si el texto en EditText coincide con una cadena (con el método contains()), así que ahora lo único que tengo que hacer es descargar todo el texto de ese sitio web a un cuerda. Pero no tengo idea de cómo hacerlo. ¿O tal vez hay un método con el que puedo verificar si un sitio web contiene una determinada palabra sin descargar el texto del sitio web a una cadena?Descargar un sitio web a una cadena

¡Gracias!

(. Por cierto, no estoy Inglés así que los PLZ me perdono si he cometido algunos errores relacionados con el lenguaje)

@androider no puedo publicar mi código en el cuadro de comentarios por lo que aquí está:

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

Estas son las propiedades del botón:

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

Respuesta

5

Puede obtener el texto usando InputStream Reader de esta manera.

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

También añadir un permiso adicional en sus aplicaciones archivo de manifiesto:

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

// ====================== ====== EDITAR ================================ //

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

Gracias para su respuesta, pero parece no funcionar correctamente, ¿qué estoy haciendo mal: – Xander

+0

Ok, soy nuevo aquí, ¿cómo puedo publicar mi código? :) – Xander

+0

Puedes publicarlo haciendo clic en la opción Editar ... y pegar allí tu código ... –

1

Ésta es una buena pregunta relacionada Java: How do you Programmatically Download a Webpage in Java

Luego puede implementar cualquier método que elija en su aplicación. Una cosa a tener en cuenta es que deberá habilitar el permiso INTERNET en su aplicación para poder acceder a Internet.

Cuestiones relacionadas