在Android应用程序中解析html

时间:2014-07-11 09:15:32

标签: android parsing jsoup

当我试图获得网站标题stackoverflow一切顺利,但当我试图获得标题beastinvest.su没有任何反应。是什么原因?

public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    Button butTest;
    TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butTest = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.textView);
        new MyParser().execute("http://beastinvest.su/");

    }

    public class MyParser extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... links) {
            Document doc = null;
            String title = null;
            try {
                doc = Jsoup.connect(links[0]).get();
                title = doc.title();
                textView.setText(title);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }
}

抱歉我的英文不好


问题是网站AntiDDOS - 查询返回&#34;访问此网站需要使用cookies并javacript您的浏览器。&#34;

2 个答案:

答案 0 :(得分:1)

提供的网站(beastinvest.su)有俄文标题,包含<meta charset="windows-1251"。我想在解析例程期间,Jsoup默认使用UTF-8编码。

答案 1 :(得分:0)

Hi use below code,


   public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    Button butTest;
    TextView textView;
    String title;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        butTest = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.textView);
        new MyParser().execute("http://beastinvest.su/");

    }

    public class MyParser extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... links) {
            Document doc = null;

            try {
                doc = Jsoup.connect(links[0]).get();
                title = doc.title();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }
}
   public void onPostExecute(String result) {
       textView.setText(title);
   }