O
O
Olzhas Ilyubayev2014-04-19 00:35:49
Java
Olzhas Ilyubayev, 2014-04-19 00:35:49

Why doesn't jsoup connect work in android?

Hey!
I'm trying to parse a page using jsoup, but jsoup.connect crashes with an error. I made permission in the manifest:
I look in the logs - I don’t understand anything! Maybe someone can help?

Source
package hondrus.helloworld;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
  
  Button btnOk;
  TextView tvInfo;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnOk = (Button) findViewById(R.id.btnOk);
        tvInfo = (TextView) findViewById(R.id.textView1);
        
        btnOk.setOnClickListener(this);

    }

aa
  @Override
  public void onClick(View v) {
    switch(v.getId()){
    case R.id.btnOk:
      
      Document doc = null;
      try {
        doc = Jsoup.connect("http://google.ru")
            .userAgent("Mozilla")
              .get();
      } catch (IOException e){
        e.printStackTrace();
      }
      
      String title = doc.title();
      tvInfo.setText(title);
      
      break;
    default: 
      break;
    } 
    
  }   

}
Logs
04-19 03:30:17.411: W/ActivityThread(13454): Application hondrus.helloworld can be debugged on port 8100...
04-19 03:30:17.491: I/Adreno-EGL(13454): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
04-19 03:30:17.511: D/OpenGLRenderer(13454): Enabling debug mode 0
04-19 03:30:18.551: D/dalvikvm(13454): GC_FOR_ALLOC freed 239K, 2% free 17022K/17292K, paused 9ms, total 9ms
04-19 03:30:18.591: D/AndroidRuntime(13454): Shutting down VM
04-19 03:30:18.591: W/dalvikvm(13454): threadid=1: thread exiting with uncaught exception (group=0x41c9cba8)
04-19 03:30:18.591: E/AndroidRuntime(13454): FATAL EXCEPTION: main
04-19 03:30:18.591: E/AndroidRuntime(13454): Process: hondrus.helloworld, PID: 13454
04-19 03:30:18.591: E/AndroidRuntime(13454): android.os.NetworkOnMainThreadException
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:439)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at org.jsoup.helper.HttpConnection.get(HttpConnection.java:167)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at hondrus.helloworld.MainActivity.onClick(MainActivity.java:50)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.view.View.performClick(View.java:4438)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.view.View$PerformClick.run(View.java:18422)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.os.Handler.handleCallback(Handler.java:733)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.os.Handler.dispatchMessage(Handler.java:95)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.os.Looper.loop(Looper.java:136)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at java.lang.reflect.Method.invokeNative(Native Method)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at java.lang.reflect.Method.invoke(Method.java:515)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-19 03:30:18.591: E/AndroidRuntime(13454): 	at dalvik.system.NativeStart.main(Native Method)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sp0tted_0wl, 2014-04-19
@ilyubayev

Disable StrickMode or use RoboSpice or AsyncTask to network. But remember: that StrickMode is just for debugging convenience. In a real project, you still have to move all "heavy" operations to a separate thread.

B
bimeg, 2014-04-19
@bimeg

android.os.NetworkOnMainThreadException - Can't surf the network on the main thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question