【问题标题】:Downloading a website to a string将网站下载到字符串
【发布时间】:2012-07-20 09:53:13
【问题描述】:

完成了一些基本教程后,我开始在 Eclipse 中制作我的第一个真正的 android 应用程序。我希望这个应用程序检查 EditText 中的文本是否与网站上的文本匹配(这个:http://www.augustinianum.eu/roosterwijzigingen/14062012.pdf(它包含我学校的时间表更改))。我已经找到了如何让应用程序检查 EditText 中的文本是否与字符串匹配(使用方法 contains()),所以现在我唯一需要做的就是将该网站的所有文本下载到细绳。但我不知道该怎么做。或者是否有一种方法可以检查网站是否包含某个单词,而无需将网站的文本下载到字符串中。

谢谢!

(顺便说一句,我不是英语,如果我犯了一些与语言相关的错误,请原谅我。)

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

【问题讨论】:

标签: android eclipse web download contain


【解决方案1】:

这是一个很好的相关 Java 问题:How do you Programmatically Download a Webpage in Java

然后,您可以在应用中实施您选择的任何方法。需要注意的一点是,您需要在您的应用上启用INTERNET 权限才能访问互联网。

【讨论】:

    【解决方案2】:

    您可以像这样使用 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();
            }
    

    【讨论】:

    • 感谢您的回复,但似乎无法正常工作,我做错了什么:
    • 啊,搞定了,我上传一个模拟器的小视频
    • 进度对话框不显示。它说我没有任何时间表更改,但我做了(字符串应该与 editText 匹配,但它没有。Toast 没有消失。我该怎么办?
    • 你的 checkScheduleChange 方法没有被调用..所以根本没有比较发生
    • 我想我发现了问题:字符串 str 说:%%EOF。
    猜你喜欢
    • 2012-08-29
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多