-1

I am on Android,using Jsoup library.

On running the MainActvity class having code below:

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class FineMain extends AppCompatActivity {

    entryPoint entryPointObj = new entryPoint();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fine_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        test();
    }
    public void test(){
        Document doc;
        String url = "http://tribune.com.pk/story/1136693/srk-shares-felt-daughter-suhanas-swimsuit-picture-going-viral/";
        try {
            doc = Jsoup.connect(url).timeout(20 * 1000).userAgent("Chrome").get();
            System.out.println(doc.text());
            Elements links = doc.select("h1");
            System.out.println(links.text());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fine_main, menu);
//        entryPointObj.ret();

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

It is giving this error. I just add a function of of Jsoup. But, the function added in the MainActivity class is working efficiently in Eclipse IDE.

08-11 07:25:25.132 10704-10704/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.awais.fine, PID: 10704
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.awais.fine/com.example.awais.fine.FineMain}: android.os.NetworkOnMainThreadException
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:136)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: android.os.NetworkOnMainThreadException
                                                       at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
                                                       at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
                                                       at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
                                                       at java.net.InetAddress.getAllByName(InetAddress.java:214)
                                                       at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
                                                       at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
                                                       at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
                                                       at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
                                                       at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
                                                       at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
                                                       at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
                                                       at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
                                                       at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:563)
                                                       at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
                                                       at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
                                                       at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216)
                                                       at com.example.awais.fine.FineMain.test(FineMain.java:31)
                                                       at com.example.awais.fine.FineMain.onCreate(FineMain.java:25)
                                                       at android.app.Activity.performCreate(Activity.java:5318)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:136) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                       at dalvik.system.NativeStart.main(Native Method) 

Any guidelines?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Humty
  • 1,321
  • 3
  • 18
  • 35
  • Yes, your code works independently of Android in Eclipse because the OS isn't preventing you from writing blocking code on the UI thread. You are welcome to use many various HTTP libraries for Android and give Jsoup the string response. Otherwise, you need to use an AsyncTask. – OneCricketeer Aug 11 '16 at 15:58

1 Answers1

1

You are trying to access to Network on the main thread and it is not possible on Android.

Take a look at the AsyncTask class, which allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers

Hope it helps,

jos
  • 1,070
  • 12
  • 22