下载一个网站的字符串字符串、网站

2023-09-06 10:33:33 作者:狂野男孩

已经做了一些基本的教程,我开始做在eclipse我第一次真正的Andr​​oid应用程序。我想这个应用程序,以检查是否在一个EditText文本网站上的文字(匹配这个: HTTP ://www.augustinianum.eu/roosterwijzigingen/14062012.pdf (它包含了我的学校的日程安排变化))。我已经找到了如何让应用程序检查,如果在的EditText文本字符串相匹配(用方法包含()),所以现在我需要做的唯一的事情是所有网站的文本下载到串。但我不知道怎么样。或者是有可能,我可以检查,如果一个网站包含的某个词,而无需下载网站的文本字符串的方法。

感谢您!

(顺便说一句,我不是英语,所以嘿原谅我,如果我做了一些语言相关的错误。)

@androider我不能发布我的code在评论栏中所以在这里,它是:

 包me.moop.mytwitter;

进口java.io.BufferedReader中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.net.MalformedURLException;
进口的java.net.URL;

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

公共类MainActivity延伸活动{

按钮mBtnCheck;
的EditText mEtxtGroup;
ProgressDialog mProgressDialog;
TwitterUser mTwitterUser;
TextView的mTxtv1;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.nicelayout3);

    mBtnCheck =(按钮)findViewById(R.id.btnCheck);
    mEtxtGroup =(EditText上)findViewById(R.id.etxtGroup);
    mTxtv1 =(TextView中)findViewById(R.id.textView1);

}

  公共无效checkScheduleChange(查看视图){
    如果(查看== mBtnCheck){
        弦乐群;
        。组= mEtxtGroup.getText()的toString();
        如果(group.length()大于0){
            mProgressDialog =新ProgressDialog(本);
            mProgressDialog.setMessage(Bezig满足checken VOOR roosterwijzigingen ......);
            mProgressDialog.show();
            尝试
            {
                网址URL =新的URL(http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf);
                的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
                字符串str;
                而((海峡= in.readLine())!= NULL){
                    mProgressDialog.dismiss();
                   如果(str.contains(mEtxtGroup.getText()的toString())){
                       Toast.makeText(这一点,ühebt EEN roosterwijziging,Toast.LENGTH_LONG).show();
                   }
                   其他{
                       Toast.makeText(这一点,U hebt吉恩roosterwijzigingen,Toast.LENGTH_LONG).show();
                   }
                }
                附寄();
            }赶上(MalformedURLException异常E){
                Toast.makeText(这一点,二是EEN FOUT opgetreden,probeer opniew,Toast.LENGTH_LONG).show();
            }赶上(IOException异常E){
                Toast.makeText(这一点,二是EEN FOUT opgetreden,probeer opniew,Toast.LENGTH_LONG).show();
            }
        }
        其他{
            Toast.makeText(此,Toast.LENGTH_LONG在Voer EEN KLAS).show();
        }
    }
  }
}
 

下面是按钮的属性:

 <按钮
        机器人:ID =@ + ID / btnCheck
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:可点击=真
        机器人:的onClick =checkScheduleChange
        机器人:文本=检查>
 
字符串替换工具下载 画龙字串替换器下载 v1.1.2 绿色版

解决方案

您可以使用InputStream的阅读器这样的文字。

 尝试
{
    网址URL =新的URL(http://yourwebpage.com);
    //读取所有服务器返回的文本
    的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
    字符串str;
    而((海峡= in.readLine())!= NULL)
    {
     // str是一行文本;的readLine()条换行符(S)
     //可以使用含有方法在这里。
       如果(str.contains(editText.getText()的toString))
        {
          你可以在这里完成你的逻辑!!!!!
        }
    }
    附寄();
}赶上(MalformedURLException异常E){
}赶上(IOException异常E){
}
 

另外,在您的应用程序添加一个额外的权限清单文件:

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
 

// ============================编辑============= =================== //

 如果(group.length()0)
  {
            mProgressDialog =新ProgressDialog(本);
            mProgressDialog.setMessage(Bezig满足checken VOOR roosterwijzigingen ......);
            mProgressDialog.show();
            尝试
            {
                网址URL =新的URL(http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf);
                的BufferedReader在=新的BufferedReader(新的InputStreamReader(url.openStream()));
                字符串str;
                而((海峡= in.readLine())!= NULL){

                   如果(str.contains(mEtxtGroup.getText()的toString())){

                     如果(mProgressDialog.isShowing())
                     {
                        mProgressDialog.dismiss();
                     }


                     Toast.makeText(这一点,ühebt EEN roosterwijziging,Toast.LENGTH_LONG).show();
                     打破;
                   }
                }
                附寄();
            }赶上(MalformedURLException异常E){
                Toast.makeText(这一点,二是EEN FOUT opgetreden,probeer opniew,Toast.LENGTH_LONG).show();
            }赶上(IOException异常E){
                Toast.makeText(这一点,二是EEN FOUT opgetreden,probeer opniew,Toast.LENGTH_LONG).show();
            }

                     如果(mProgressDialog.isShowing())
                     {
                        mProgressDialog.dismiss();
                     }
        }
        其他{
            Toast.makeText(此,Toast.LENGTH_LONG在Voer EEN KLAS).show();
        }
 

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

解决方案

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