我正在开发一个Android应用程序,该应用程序正在使用Cling执行UPNP扫描。它记录它接收数据包,但随后它因Jetty的NoClassDefFound异常而崩溃。
下面是我用来执行扫描的代码
ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
upnpService = (AndroidUpnpService)service;
upnpService.getRegistry().addListener(listener);
upnpService.getControlPoint().search();
}
@Override
public void onServiceDisconnected(ComponentName name)
{
upnpService = null;
}
};
public HueBridgeSearch(Context context, Activity activity)
{
this.context = context;
this.activity = activity;
}
public void startBridgeSearch()
{
// Fix the logging integration between java.util.logging and Android internal logging
org.seamless.util.logging.LoggingUtil.resetRootHandler(
new FixedAndroidLogHandler()
);
Logger.getLogger("org.fourthline.cling").setLevel(Level.ALL);
listener = new RegistryListener()
{
@Override
public void remoteDeviceDiscoveryStarted(Registry registry, RemoteDevice remoteDevice)
{
Log.d(TAG, "Remote Device Discovery Started: " + remoteDevice.getDisplayString());
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void remoteDeviceDiscoveryFailed(Registry registry, RemoteDevice remoteDevice, Exception e)
{
Log.d(TAG, "Remote device discovery failed: " + remoteDevice.getDisplayString());
Log.d(TAG, "Remote device discovery failed: " + e.toString());
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void remoteDeviceAdded(Registry registry, RemoteDevice remoteDevice)
{
Log.d(TAG, "Added Found Device: " + remoteDevice.getDisplayString());
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void remoteDeviceUpdated(Registry registry, RemoteDevice remoteDevice)
{
Log.d(TAG, "Updated Found Device: " + remoteDevice.getDisplayString());
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void remoteDeviceRemoved(Registry registry, RemoteDevice remoteDevice)
{
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void localDeviceAdded(Registry registry, LocalDevice localDevice)
{
Log.d(TAG, "Local Added Found Device: " + localDevice.getDisplayString());
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void localDeviceRemoved(Registry registry, LocalDevice localDevice)
{
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void beforeShutdown(Registry registry)
{
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
@Override
public void afterShutdown()
{
Toast.makeText(context, "GOT SOMETHING", Toast.LENGTH_SHORT).show();
}
};
Intent intent = new Intent(activity, AndroidUpnpServiceImpl.class);
//intent.setClassName("org.fourthline.cling.android", "org.fourthline.cling.android.AndroidUpnpServiceImpl");
boolean success = context.bindService(intent,
serviceConnection, Context.BIND_AUTO_CREATE);
}
下面是我的渐变构建文件依赖
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/cling-core-2.1.0.jar')
compile files('libs/seamless-util-1.1.1.jar')
compile 'com.android.support:appcompat-v7:23.3.0'
compile files('libs/jetty-server-9.2.15.v20160210.jar')
compile files('libs/jetty-servlet-9.2.15.v20160210.jar')
compile files('libs/jetty-servlets-9.2.15.v20160210.jar')
compile files('libs/seamless-http-1.1.1.jar')
}
它编译得很好,但在运行时出现以下异常
/AndroidRuntime: FATAL EXCEPTION: main Process: com.BoardiesITSolutions.HueRulesAdvanced, PID: 9393
java.lang.NoClassDefFoundError: org.eclipse.jetty.server.Server at org.fourthline.cling.transport.impl.jetty.JettyServletContainer.resetServer(JettyServletContainer.java:165) at org.fourthline.cling.transport.impl.jetty.JettyServletContainer.<init>(JettyServletContainer.java:57) at org.fourthline.cling.transport.impl.jetty.JettyServletContainer.<clinit>(JettyServletContainer.java:55)at org.fourthline.cling.android.AndroidUpnpServiceConfiguration.createStreamServer(AndroidUpnpServiceConfiguration.java:110)
at org.fourthline.cling.transport.RouterImpl.startAddressBasedTransports(RouterImpl.java:427)
at org.fourthline.cling.transport.RouterImpl.enable(RouterImpl.java:132)
at org.fourthline.cling.android.AndroidRouter.enable(AndroidRouter.java:91)
at org.fourthline.cling.UpnpServiceImpl.<init>(UpnpServiceImpl.java:87)
at org.fourthline.cling.android.AndroidUpnpServiceImpl$1.<init>(AndroidUpnpServiceImpl.java:54)
at org.fourthline.cling.android.AndroidUpnpServiceImpl.onCreate(AndroidUpnpServiceImpl.java:54)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
at android.app.ActivityThread.-wrap4(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
经过反复试验,我终于找到了答案。不是将jar文件下载到lib文件夹并引用gradle文件中的jar,而是将build.gradle从:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/cling-core-2.1.0.jar')
compile files('libs/seamless-util-1.1.1.jar')
compile 'com.android.support:appcompat-v7:23.3.0'
compile files('libs/jetty-server-9.2.15.v20160210.jar')
compile files('libs/jetty-servlet-9.2.15.v20160210.jar')
compile files('libs/jetty-servlets-9.2.15.v20160210.jar')
compile files('libs/seamless-http-1.1.1.jar')
}
至
dependencies {
compile 'com.android.support:appcompat-v7:23.3.0'
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/seamless-util-1.1.1.jar')
compile files('libs/seamless-http-1.1.1.jar')
compile files('libs/seamless-xml-1.1.1.jar')
compile files('libs/cling-core-2.1.0.jar')
compile 'org.eclipse.jetty:jetty-server:8+'
compile 'org.eclipse.jetty:jetty-servlet:8+'
compile 'org.eclipse.jetty:jetty-client:8+'
}