ANDROID - 无法连接到网站

时间:2015-12-01 15:42:33

标签: android jsoup

我尝试连接网站以使用JSOUP解析一些元素。

这是错误日志

    12-01 17:34:24.938 591-870/? E/Watchdog: !@Sync 4816
12-01 17:34:54.947 591-870/? E/Watchdog: !@Sync 4817
12-01 17:35:24.946 591-870/? E/Watchdog: !@Sync 4818
12-01 17:35:44.655 2856-2856/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-01 17:35:44.655 2856-2856/? E/android.os.Debug: failed to load memtrack module: -2
12-01 17:35:45.136 591-709/? E/PackageManager: parse pkg : /data/app/vmdl926348494.tmp
12-01 17:35:45.156 591-2879/? E/PackageManager: dexopt start
12-01 17:35:49.060 591-2879/? E/PackageManager: dexopt end
12-01 17:35:49.901 4527-4527/? E/SamsungIME: mOCRHelper is null
12-01 17:35:49.901 4527-4527/? E/SamsungIME: mOCRHelper is null
12-01 17:35:51.882 2919-2919/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-01 17:35:51.882 2919-2919/? E/android.os.Debug: failed to load memtrack module: -2
12-01 17:35:52.183 975-975/? E/NetworkScheduler.SchedulerReceiver: Invalid parameter app
12-01 17:35:52.183 975-975/? E/NetworkScheduler.SchedulerReceiver: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
12-01 17:35:52.693 2956-2973/ytu.ytu2 E/GMPM: getGoogleAppId failed with status: 10
12-01 17:35:52.693 2956-2973/ytu.ytu2 E/GMPM: Uploading is not possible. App measurement disabled
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime: FATAL EXCEPTION: main
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime: Process: ytu.ytu2, PID: 2956
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{ytu.ytu2/ytu.ytu2.MainActivity}: android.os.NetworkOnMainThreadException
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:166)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5584)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:  Caused by: android.os.NetworkOnMainThreadException
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:512)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at ytu.ytu2.MainActivity.connect(MainActivity.java:57)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at ytu.ytu2.MainActivity.onCreate(MainActivity.java:49)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5447)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:166) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5584) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
12-01 17:35:52.923 2956-2956/ytu.ytu2 E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
12-01 17:35:53.284 591-2999/? E/android.os.Debug: !@Dumpstate > sdumpstate -k -t -z -d -m 2956 -o /data/log/dumpstate_app_error
12-01 17:35:54.945 591-870/? E/Watchdog: !@Sync 4819
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter: Error acquiring lock: /data/data/com.facebook.katana/app_state_logs/e9d2c7b4-fcc2-42a5-a2d9-e57277e8cb06.txt
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter: java.io.IOException: fcntl failed: EAGAIN (Try again)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.nio.FileChannelImpl.basicLock(FileChannelImpl.java:123)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.nio.FileChannelImpl.tryLock(FileChannelImpl.java:177)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.nio.channels.FileChannel.tryLock(FileChannel.java:587)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.analytics.reporters.AppStateReporter.a(AppStateReporter.java:141)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.analytics.reporters.AppStateReporter.c(AppStateReporter.java:94)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.analytics.reporters.AppStateReporter.a(AppStateReporter.java:55)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.noncriticalinit.NonCriticalInitializer.a(NonCriticalInitializer.java:103)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.noncriticalinit.NonCriticalInitializer$2.run(NonCriticalInitializer.java:116)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.appchoreographer.DefaultAppChoreographer$5.run(DefaultAppChoreographer.java:384)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.executors.WrappingExecutorService$1.run(WrappingExecutorService.java:77)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.executors.DefaultConstrainedListeningExecutorService$Worker.run(DefaultConstrainedListeningExecutorService.java:327)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.executors.NamedThreadFactory$1.run(NamedThreadFactory.java:42)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.lang.Thread.run(Thread.java:841)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:  Caused by: libcore.io.ErrnoException: fcntl failed: EAGAIN (Try again)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at libcore.io.Posix.fcntlFlock(Native Method)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at libcore.io.ForwardingOs.fcntlFlock(ForwardingOs.java:54)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.nio.FileChannelImpl.basicLock(FileChannelImpl.java:121)
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.nio.FileChannelImpl.tryLock(FileChannelImpl.java:177) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.nio.channels.FileChannel.tryLock(FileChannel.java:587) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.analytics.reporters.AppStateReporter.a(AppStateReporter.java:141) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.analytics.reporters.AppStateReporter.c(AppStateReporter.java:94) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.analytics.reporters.AppStateReporter.a(AppStateReporter.java:55) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.noncriticalinit.NonCriticalInitializer.a(NonCriticalInitializer.java:103) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.noncriticalinit.NonCriticalInitializer$2.run(NonCriticalInitializer.java:116) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.appchoreographer.DefaultAppChoreographer$5.run(DefaultAppChoreographer.java:384) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.executors.WrappingExecutorService$1.run(WrappingExecutorService.java:77) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.executors.DefaultConstrainedListeningExecutorService$Worker.run(DefaultConstrainedListeningExecutorService.java:327) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at com.facebook.common.executors.NamedThreadFactory$1.run(NamedThreadFactory.java:42) 
12-01 17:35:57.178 2986-3077/? E/fb4a(:dash):AppStateReporter:     at java.lang.Thread.run(Thread.java:841) 
12-01 17:35:57.638 975-975/? E/NetworkScheduler.SchedulerReceiver: Invalid parameter app
12-01 17:35:57.638 975-975/? E/NetworkScheduler.SchedulerReceiver: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
12-01 17:35:58.389 157-396/? E/cutils: Failed to mkdirat(/storage/extSdCard/Android): Read-only file system
12-01 17:35:58.389 157-396/? E/cutils: Failed to mkdirat(/storage/extSdCard/Android): Read-only file system
12-01 17:36:00.911 3288-3288/? E/dalvikvm: Could not find class 'android.speech.tts.Voice', referenced from method com.google.android.tts.service.GoogleTTSService.onGetVoices

这一行57:

doc = Jsoup.connect(URL).get();
这个49

 connect();

调用connect方法建立连接。

check for validity of URL in java. so as not to crash on 404 error

我在这里尝试了这个建议,但没有奏效。一旦屏幕cmes,它就会与toast it is stopped

崩溃

也许我可以使用另一种东西而不是jsoup但我在java中的eclipse中做了一些代码所以我不知道我是否可以迁移它们,例如okhttp。

这是我的代码。大多数进口都没用。

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;


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



  public class MainActivity extends Activity {
      String URL="https://stackoverflow.com/";
      Document doc;
      String html;//from page, parsed part



public static TextView textView;
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          setContentView(R.layout.activity_main);

          textView = (TextView) findViewById (R.id.txt);
        //  textView.setText(" coor ");
       //   WebDriver driver = new AndroidDriver(getActivity());

          connect();
          }



public void connect (){

          try {
              doc = Jsoup.connect(URL).get();
              textView.append(" con ");
          } catch (IOException e) {
              e.printStackTrace();textView.append(" catch ");
          }
         // html = doc.select("script").get(10).toString();
        //  textView.append(" selected ");
      }


              }

1 个答案:

答案 0 :(得分:0)

您不能在主线程中进行该连接。您必须使用另一个线程或实现Async类来执行此操作。 Android避免使用mainThread建立连接,以便UI不会被冻结。

AsyncTask Android example

http://developer.android.com/guide/components/processes-and-threads.html