7

Having done some basic tutorials, I started making my first real android app in eclipse. I want this app to check if the text in an EditText matches the text on a website (this one: http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf (it contains my school's schedule changes)). I've found out how to make the app check if the text in the EditText matches a string (with the method contains()), so now the only thing I need to do is to download all of the text of that website to a string. But I have no idea how to. Or is there maybe a method which I can check with if a website contains a certain word without downloading the website's text to a string.

Thank You!

(BTW I'm not English so plz forgive me if I've made some language-related mistakes.)

@androider I can't post my code in the comment box so here it is:

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

Here are the button's properties:

 <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" >
Xander
  • 5,487
  • 14
  • 49
  • 77
  • You can use an HTML parser Lib like Jsoup see this answer https://stackoverflow.com/questions/238547/how-do-you-programmatically-download-a-webpage-in-java – WORKSHORE MAROC Nov 19 '21 at 08:41

2 Answers2

5

You can get the text using InputStream Reader like this.

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

Also add an additional permission in your apps Manifest file:

<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();
        }
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
  • Thank you for your reply, but it doesn't seem to work out properly, what am I doing wrong: – Xander Jul 21 '12 at 10:07
  • Ah, did it, i'll upload a little video of the emulator – Xander Jul 21 '12 at 10:22
  • The progressdialog doesn't show up. It says I haven't got any schedule change but I do (the string should match with the editText, but it doesn't. The Toast doesn't disappear. What do I do? – Xander Jul 21 '12 at 10:27
  • Your checkScheduleChange method is not called..so there is no comparison taking place at all – Haresh Chaudhary Jul 21 '12 at 10:30
  • I think i found the problem: the string str says: %%EOF. – Xander Jul 21 '12 at 10:31
  • You will have to put On click listener on the button..let's say there is a Comapre Button..then you should put a listener so that when it's clicked,the comparing method is invoked – Haresh Chaudhary Jul 21 '12 at 10:32
  • It is, I call it when btnCheck is clicked – Xander Jul 21 '12 at 10:32
  • How it is possible without declaring any type of listener on the Button??? We should declare a onClick listener like button.setOnClickListener(this); – Haresh Chaudhary Jul 21 '12 at 10:35
  • I posted the button's properties – Xander Jul 21 '12 at 10:35
  • I think the method checkScheduleChange is called, otherwise the toast wouldn't show up at all, right? – Xander Jul 21 '12 at 10:39
  • Yaa...you are Right..after going through the properties I got clear – Haresh Chaudhary Jul 21 '12 at 10:41
  • The Toast would not be seen if the ProgressBar is running..You have to dismiss the Progress bar when the condition is satisfied and Display a toast message. – Haresh Chaudhary Jul 21 '12 at 10:42
  • Ok, i'll try, but why can't I dismiss it before the if-block started? – Xander Jul 21 '12 at 10:45
  • Cause now it says dismiss twice – Xander Jul 21 '12 at 10:45
  • And i have also removed the else part in the while loop..because str contains only one line in one iteration..there are many lines and after checking one line if the string is not foung,it will toast the message..without checking futher. – Haresh Chaudhary Jul 21 '12 at 10:46
  • Replace the mProgressDialog.dismiss() by if(mProgressDialog.isShowing()){ mProgressDialog.dismiss(); }...by this,it will check that if the progressbar is active,only then go to dismiss it. – Haresh Chaudhary Jul 21 '12 at 10:48
  • Ok, but now the ProgressDialog doesn't show up at all and the website's text is still not downloaded to the string. – Xander Jul 21 '12 at 10:54
  • You are giving the path of a PDF and i am extremely Sorry as i have posted this code for websites.Please try it for some real webpage – Haresh Chaudhary Jul 21 '12 at 10:56
  • It works like a charm indeed for websites, i'll change my question to downloading a PDF page. Thank you for you time and help anyway! – Xander Jul 21 '12 at 11:02
  • Ah, found out. You deserve it bro! – Xander Jul 21 '12 at 11:21
  • For PDF...I would like to give you one Suggestion..Actually its very tough to read a PDF Document in this way..i mean just giving a link url to read it and so people now a days are developing complete application that are specially created to read just pdf document. – Haresh Chaudhary Jul 21 '12 at 11:24
1

This is a good related Java question: How do you Programmatically Download a Webpage in Java

You can then implement whatever method you choose into your app. One thing to note is that you will need to enable the INTERNET permission on your app in order to access the internet.

Community
  • 1
  • 1
telkins
  • 10,440
  • 8
  • 52
  • 79