简体   繁体   中英

Can't get website using jsoup on Android

I have a problem with getting website with jsoup on Android.

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.

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

References

you can use a httpURLconnections as an alternative and see if that works. are you getting anything as the debug output for this code?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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