0

try to show some specific part of web in web view and use jsoup library so select my Intended div and this is my code :

public class MainActivity extends AppCompatActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);
        WebView webView = (WebView) findViewById(R.id.web);
        webView.getSettings().setJavaScriptEnabled(true);

        try {
            Document document = Jsoup.connect("http://www.memaraneha.ir").get();
            Element elements=document.select("div.news-list").first();
            String html = elements.toString();
            String mime = "text/html";
            String encoding = "utf-8";
            webView.loadData(html, mime, encoding);
        } catch (IOException e) {
            e.printStackTrace();
        }

}}

but when run this got this error : android.os.NetworkOnMainThreadException

after some research i know what is this error and how must solve this with put codes in AsyncTask function with this question (and not duplicate in my case) i try this but not work and seems not possible to put this jsoup codes in AsyncTask or i dont know how must do that...

if any one can please help.

Community
  • 1
  • 1
Erfan
  • 3,059
  • 3
  • 22
  • 49

2 Answers2

4

Use Asynctask to load the document

public class MainActivity extends AppCompatActivity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);
        WebView webView = (WebView) findViewById(R.id.web);
        webView.getSettings().setJavaScriptEnabled(true);

        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... voids) {
                String html = "";
                try {
                    Document document = Jsoup.connect("http://www.memaraneha.ir").get();
                    Element elements=document.select("div.news-list").first();
                    html = elements.toString();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return html;
            }

            @Override
            protected void onPostExecute(String html) {
                String mime = "text/html; charset=utf-8";
                String encoding = "utf-8";
                webView.loadData(html, mime, encoding);
            }
        }.execute();

}}
J.R
  • 2,113
  • 19
  • 21
  • cant recognize webview in onPostExecute and after that give error for .execute(); – Erfan Dec 07 '16 at 08:11
  • Change Webview to final – J.R Dec 07 '16 at 08:13
  • ok webview error gone . but give error for .execute(); in onPostExecute . and for accept your answer please edit your code and show how Calling asynctask in on create meathod – Erfan Dec 07 '16 at 08:15
  • It's already calling to excecute the asynctask. You can see .execute(); at the end of the code. – J.R Dec 07 '16 at 08:17
  • Change Document document = Jsoup.connect("http://www.memaraneha.ir").timeout(20*1000).get(); – J.R Dec 07 '16 at 08:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129981/discussion-between-erfan-and-jithinraj). – Erfan Dec 07 '16 at 08:36
1

You cannot execute network calls on the main thread, this has been around in android for the past few years (since API 11) and it's there for a good reason.

If you execute network calls on your main thread you'll cause ANRs and skip frames and your users will uninstall your app.

you should use any method of sending the requests on a background thread (either AsyncTask or any networking library there's out there)

for more, read this article

thepoosh
  • 12,497
  • 15
  • 73
  • 132
  • i know should use AsyncTask but i try so many times for put that jsoup codes in AsyncTask but seems not work – Erfan Dec 07 '16 at 08:02
  • maybe jsoup is not the right library for networking, have you tried using [OkHttp](https://github.com/square/okhttp) or [Volley](https://developer.android.com/training/volley/index.html)? – thepoosh Dec 07 '16 at 08:03