简体   繁体   English

无法在Android上使用jsoup获取网站

[英]Can't get website using jsoup on Android

I have a problem with getting website with jsoup on Android. 我在Android上使用jsoup获取网站时遇到问题。

public class Parser
{
    Parser()
    {
        new Parser1().execute();
    }

    class Parser1 extends AsyncTask<Void, Void, Void>
    {
        String website1 = "http://google.com";
        Document doc;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

This code is not execute doInBackground method. 此代码不执行doInBackground方法。

        @Override
        protected Void doInBackground(Void... params)
        {
            try
            {
                doc = Jsoup.connect(website1).get();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            return null;
        }

And the rest of code. 和其余的代码。

        @Override
        protected void onProgressUpdate(Void... values)
        {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result)
        {
            Log.d ("OK",doc.toString());

            super.onPostExecute(result);
        }

        @Override
        protected void onCancelled()
        {
            super.onCancelled();
        }
    }
}

I tried to write code without class AsyncTask, but always on Json.connect the program was exception. 我尝试编写没有类AsyncTask的代码,但始终在Json.connect上,程序是例外。 Thanks for all replies. 感谢您的所有回复。

Try this: 尝试这个:

@Override
protected Void doInBackground(Void... params) {
   try {
       doc = GetDocument(website1);
   } catch (Exception e) {
       e.printStackTrace();
   }

   return null;
}

Click the following link to get the full implementation of GetDocument . 单击以下链接以获取GetDocument完整实现

References 参考文献

you can use a httpURLconnections as an alternative and see if that works. 您可以使用httpURLconnections作为替代方案,看看是否可行。 are you getting anything as the debug output for this code? 您得到什么作为此代码的调试输出?

Log.d ("OK",doc.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM