2013-07-17 69 views
1

我正在開發一個使用nanohttpd創建Web服務器的Android應用程序,當我運行它時它說該活動已停止這是我的代碼請幫助我任何幫助將appriciated.here去代碼:Android中的Nanohttpd服務器

package dolphin.developers.com; 


import java.io.IOException; 
import java.io.InputStream; 
import java.util.Map; 

import android.content.Context; 
import android.os.Environment; 


public class MyHTTPD extends NanoHTTPD{ 

private Context ctx; 


public MyHTTPD(Context ctx) throws IOException { 
super(8080); 
    this.ctx = ctx; 
} 


@Override 
public Response serve(String uri, Method method, 

     Map<String, String> header, Map<String, String> parms, 
     Map<String, String> files) 
     { 
     String html = null; 
     InputStream is = null; 
     try { 
      is = ctx.getAssets().open(Environment.getExternalStorageDirectory()+"/index.htm"); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     byte[] b; 
     try { 
      b = new byte[is.available()]; 
      is.read(b); 
      html = new String(b); 
     } catch (IOException e) { // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      return new NanoHTTPD.Response(html); 
     } 
} 

的logcat:

07-17 12:06:22.538: E/AndroidRuntime(1137): FATAL EXCEPTION: main 
07-17 12:06:22.538: E/AndroidRuntime(1137): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dolphin.devlopers.com/dolphin.developers.com.MyHTTPD}: java.lang.ClassCastException: dolphin.developers.com.MyHTTPD cannot be cast to android.app.Activity 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.os.Looper.loop(Looper.java:137) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at dalvik.system.NativeStart.main(Native Method) 
07-17 12:06:22.538: E/AndroidRuntime(1137): Caused by: java.lang.ClassCastException: dolphin.developers.com.MyHTTPD cannot be cast to android.app.Activity 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 
07-17 12:06:22.538: E/AndroidRuntime(1137):  ... 11 more 
+0

你有沒有在Manifest文件中聲明MyHTTPD? – mromer

+0

是的,我已經宣佈這兩個類和NanoHTTPD – Prakhar

回答

2

只能聲明,在清單擴展Android的「活動」作爲活動類。嘗試創建一個,然後在Activity生命週期(onCreate/onDestroy或onStart/onStop)嘗試啓動和停止NanoHttpd服務器。

2

MyHTTPD不是一個活動,所以你不能添加到這個清單文件

這裏有一個例子:

https://gist.github.com/komamitsu/1893396

就是這個例子中,你只需要在清單中的活動添加AndroidWebServerActivity

+0

請注意server.Start()丟失。 – lifelogger