对于我的应用程序,我想获取网站的html文本。但是我网站上的动态文本是用JavaScript编写的,所以我编写了一个Java程序,我使用" HtmlUnit"来获取文本。现在我想在我的Android应用程序中显示此文本。当我在Android-Studio中导入最新版本的HtmlUnit时发生错误。我通过在build.gradle中实现旧版本的HtmlUnit来解决它:
dependencies {
implementation 'net.sourceforge.htmlunit:htmlunit:2.12'
}
我还添加了#34;互联网许可"到AndroidManifest.xml。然后我尝试在Java中使用HtmlUnit,但是当我尝试启动应用程序时,它会立即崩溃。我的" MainActivity.java"的代码如下:
package com.example.test.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebClient webClient = new WebClient();
try {
final HtmlPage page = webClient.getPage("https://www.google.com/");
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果没有final HtmlPage page = webClient.getPage("https://www.google.com/");
,应用就可以完美无误地启动。一旦我添加此代码,应用程序就无法正常启动,当我使用调试器时,会出现以下错误(我将其放入&#34;代码风格&#34;因此可以更好地阅读):< / p>
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test.myapplication, PID: 8543
java.lang.IncompatibleClassChangeError: Superclass org.apache.http.conn.scheme.PlainSocketFactory of com.gargoylesoftware.htmlunit.SocksSocketFactory is declared final (declaration of 'com.gargoylesoftware.htmlunit.SocksSocketFactory' appears in /data/app/com.example.test.myapplication-2/split_lib_dependencies_apk.apk)
at com.gargoylesoftware.htmlunit.WebClient.createWebConnection(WebClient.java:2123)
at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:129)
at com.gargoylesoftware.htmlunit.WebClient.<init>(WebClient.java:182)
at com.example.test.myapplication.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
我是否需要更改HtmlUnit代码?我已经尝试过,但代码是&#34; readonly&#34;。 或者我可以做什么/使用否则? (也许我的代码中有错误?)
提前谢谢! Venatorc