我想在Android中创建一个HTTP服务器。我想在这个程序中使用com.sun.net.httpserver。在您看来,是否可以在Android程序中使用此包? 我写了一个服务来做这件事。我的节目是休闲的:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
public class HSMainService extends Service {
//region constants
public static final int MY_NOTIFICATION_ID = 1;
public static final int SERVERPORT = 8080;
//endregion
//************************
//region instance variables
private String TAG;
private Uri soundURI = Uri
.parse("android.resource://com.ebcompany.hs4/"
+ R.raw.alarm_rooster);
private long[] mVibratePattern = { 0, 200, 200, 300 };
private HttpServer server;
//endregion
//************************
//region constructors
public HSMainService() {
//TAG = getApplicationContext().getString(R.string.log_message_tag);
}
//endregion
//************************
//region override methods
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
//handleCommand(intent);
Log.i(TAG, "onStart");
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
Log.i(TAG, "onStartCommand");
//Intent in = new Intent(getApplicationContext(), TestActivity.class);
//in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//startActivity(in);
//Toast.makeText(getApplicationContext(), "onStartCommand", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
Context cntx = getApplicationContext();
TAG = cntx.getString(R.string.log_message_tag);
Log.i(TAG, "Service creating");
final Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext())
.setContentInfo("HS4")
.setSmallIcon(R.drawable.icon)
.setContentTitle(cntx.getString(R.string.notification_content_title))
.setContentText(cntx.getString(R.string.notification_content_text))
.setTicker(cntx.getString(R.string.notification_tiker_text))
.setSound(soundURI)
.setOngoing(true)
.setLargeIcon((((BitmapDrawable) this.getResources().getDrawable(R.drawable.fire_eye_alien)).getBitmap()));
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setWhen(System.currentTimeMillis());
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
try {
server = HttpServer.create(new InetSocketAddress(SERVERPORT), 0);
server.createContext("IS2", new IS2Handler());
server.createContext("DSP", new DSPHandler());
server.setExecutor(Executors.newFixedThreadPool(0x5) ); // creates a default executor
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "Service destroying");
//server.stop(0x1);
}
//endregion
//************************
}
当我运行此程序时,我收到此错误:
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 I/dalvikvm﹕ Could not find method sun.misc.Service.providers, referenced from method com.sun.net.httpserver.spi.HttpServerProvider.loadProviderAsService
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 W/dalvikvm﹕ VFY: unable to resolve static method 421: Lsun/misc/Service;.providers (Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/Iterator;
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 E/dalvikvm﹕ Could not find class 'sun.misc.ServiceConfigurationError', referenced from method com.sun.net.httpserver.spi.HttpServerProvider.loadProviderFromProperty
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 W/dalvikvm﹕ VFY: unable to resolve new-instance 155 (Lsun/misc/ServiceConfigurationError;) in Lcom/sun/net/httpserver/spi/HttpServerProvider;
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 E/dalvikvm﹕ Could not find class 'sun.misc.ServiceConfigurationError', referenced from method com.sun.net.httpserver.spi.HttpServerProvider.loadProviderFromProperty
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 W/dalvikvm﹕ VFY: unable to resolve new-instance 155 (Lsun/misc/ServiceConfigurationError;) in Lcom/sun/net/httpserver/spi/HttpServerProvider;
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 E/dalvikvm﹕ Could not find class 'sun.misc.ServiceConfigurationError', referenced from method com.sun.net.httpserver.spi.HttpServerProvider.loadProviderFromProperty
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 W/dalvikvm﹕ VFY: unable to resolve new-instance 155 (Lsun/misc/ServiceConfigurationError;) in Lcom/sun/net/httpserver/spi/HttpServerProvider;
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 E/dalvikvm﹕ Could not find class 'sun.misc.ServiceConfigurationError', referenced from method com.sun.net.httpserver.spi.HttpServerProvider.loadProviderFromProperty
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 W/dalvikvm﹕ VFY: unable to resolve new-instance 155 (Lsun/misc/ServiceConfigurationError;) in Lcom/sun/net/httpserver/spi/HttpServerProvider;
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xacd19b20)
08-02 04:24:37.233 2994-2994/com.ebcompany.hs4 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ebcompany.hs4, PID: 2994
java.lang.NoClassDefFoundError: sun.misc.Service
at com.sun.net.httpserver.spi.HttpServerProvider.loadProviderAsService(HttpServerProvider.java:82)
at com.sun.net.httpserver.spi.HttpServerProvider.access$200(HttpServerProvider.java:27)
at com.sun.net.httpserver.spi.HttpServerProvider$1.run(HttpServerProvider.java:144)
at java.security.AccessController.doPrivileged(AccessController.java:45)
at com.sun.net.httpserver.spi.HttpServerProvider.provider(HttpServerProvider.java:139)
at com.sun.net.httpserver.HttpServer.create(HttpServer.java:110)
at com.ebcompany.hs4.HSMainService.onCreate(HSMainService.java:93)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2572)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
我该如何解决这个问题?
答案 0 :(得分:1)
从1.6开始,JDK应该可以使用它。否则,您可以单独添加构建路径。请参阅以下链接:
您可以从http://download.java.net/maven/2/com/sun/net/httpserver/http/20070405/http-20070405.jar
下载