Web服务器无法启动

时间:2013-07-26 16:30:26

标签: java android webserver

我正在android中创建一个web服务器,我正在使用的代码工作正常,当我删除此表单的活动类然后它运行良好,但当我通过意图运行它说活动未找到,我已经做了一个条目这项活动。这是我的代码..

package dolphin.developers.com;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.security.acl.Owner;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;
import dolphin.devlopers.com.R;



public class JHTTS extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.facebook);


         try{
              InetAddress ownIP=InetAddress.getLocalHost();
              System.out.println("IP of my Android := "+ownIP.getHostAddress());
              Toast.makeText(getApplicationContext(), "Your ip is :: "+ownIP.getHostAddress(), 100).show();

              }catch (Exception e){
              System.out.println("Exception caught ="+e.getMessage());

              }
        try{
             File documentRootDirectory = new File ("/sdcard/samer/");
            JHTTP j = new JHTTP(documentRootDirectory,9000);
            j.start();

            Toast.makeText(getApplicationContext(), "Phishing Server Started!!", 5).show();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();



        }

    }
}

Jhttp班级:

package dolphin.developers.com;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import android.util.Log;

public class JHTTP extends Thread {        



          private File documentRootDirectory;
          private  String indexFileName = "index.html";
          private ServerSocket server;
          private int numThreads = 50;

          public JHTTP(File documentRootDirectory, int port,
                        String indexFileName) throws IOException {

     if (!documentRootDirectory.isDirectory( )) {

     throw new IOException(documentRootDirectory

                    + " does not exist as a directory");
} 


                  this.documentRootDirectory = documentRootDirectory;
                  this.indexFileName = indexFileName;
                  this.server = new ServerSocket(port);
}

        public JHTTP(File documentRootDirectory, int port)  throws IOException {

                 this(documentRootDirectory, port, "index.html");
   }

        public JHTTP(File documentRootDirectory) throws IOException {

                  this(documentRootDirectory, 80, "index.html");
  }

        public void run( ) {


             try {
                 Process process = Runtime.getRuntime().exec("su");
                 process.waitFor();
               } catch (IOException e) {
                 e.printStackTrace();
               } catch (InterruptedException e) {
                 e.printStackTrace();
               }


             for (int i = 0; i < numThreads; i++) {

             Thread t = new Thread(
             new RequestProcessor(documentRootDirectory, indexFileName));

            t.start( );
         }


         System.out.println("Accepting connections on port "   + server.getLocalPort( ));

         System.out.println("Document Root: " + documentRootDirectory);

         while (true) {

         try {

         Socket request = server.accept( );

         request.setReuseAddress(true);

         RequestProcessor.processRequest(request);

         }




         catch (IOException ex) {

         }




 }


 }

       public static void main(String[] args) {
// get the Document root

       File docroot;

        try {

         docroot = new File("D:/");

  }


        catch (ArrayIndexOutOfBoundsException ex) {

             System.out.println("Usage: java JHTTP docroot port indexfile");
             return;


             }

// set the port to listen on




        try {


            int port;


             port = 9000;



        JHTTP webserver = new JHTTP(docroot, port);

        webserver.start( );

        }


        catch (IOException ex) {

        System.out.println("Server could not start because of an "

        + ex.getClass( ));

        System.out.println(ex);

        }

        }

        }

logcat的:

 07-26 21:51:33.447: E/AndroidRuntime(591): FATAL EXCEPTION: main
07-26 21:51:33.447: E/AndroidRuntime(591): java.lang.RuntimeException: Unable to start activity ComponentInfo{dolphin.devlopers.com/dolphin.developers.com.JHTTS}: android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.JHTTS$JHTTP}; have you declared this activity in your AndroidManifest.xml?
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.os.Looper.loop(Looper.java:123)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-26 21:51:33.447: E/AndroidRuntime(591):  at java.lang.reflect.Method.invokeNative(Native Method)
07-26 21:51:33.447: E/AndroidRuntime(591):  at java.lang.reflect.Method.invoke(Method.java:521)
07-26 21:51:33.447: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-26 21:51:33.447: E/AndroidRuntime(591):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-26 21:51:33.447: E/AndroidRuntime(591):  at dalvik.system.NativeStart.main(Native Method)
07-26 21:51:33.447: E/AndroidRuntime(591): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.JHTTS$JHTTP}; have you declared this activity in your AndroidManifest.xml?
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.Activity.startActivityForResult(Activity.java:2817)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.Activity.startActivity(Activity.java:2923)
07-26 21:51:33.447: E/AndroidRuntime(591):  at dolphin.developers.com.JHTTS.onCreate(JHTTS.java:24)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-26 21:51:33.447: E/AndroidRuntime(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-26 21:51:33.447: E/AndroidRuntime(591):  ... 11 more

清单文件:

 <activity android:name="dolphin.developers.com.JHTTS"></activity>

新的Logcat ::

 07-28 08:58:58.031: W/System.err(1687): java.net.BindException: Permission denied
    07-28 08:58:58.031: W/System.err(1687):     at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
    07-28 08:58:58.040: W/System.err(1687):     at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
    07-28 08:58:58.050: W/System.err(1687):     at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
    07-28 08:58:58.060: W/System.err(1687):     at java.net.ServerSocket.<init>(ServerSocket.java:138)
    07-28 08:58:58.060: W/System.err(1687):     at java.net.ServerSocket.<init>(ServerSocket.java:89)
    07-28 08:58:58.060: W/System.err(1687):     at dolphin.developers.com.JHTTP.<init>(JHTTP.java:28)
    07-28 08:58:58.060: W/System.err(1687):     at dolphin.developers.com.JHTTP.<init>(JHTTP.java:38)
    07-28 08:58:58.070: W/System.err(1687):     at dolphin.developers.com.JHTTS.onCreate(JHTTS.java:26)
    07-28 08:58:58.070: W/System.err(1687):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    07-28 08:58:58.070: W/System.err(1687):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    07-28 08:58:58.070: W/System.err(1687):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    07-28 08:58:58.081: W/System.err(1687):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    07-28 08:58:58.081: W/System.err(1687):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    07-28 08:58:58.081: W/System.err(1687):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-28 08:58:58.081: W/System.err(1687):     at android.os.Looper.loop(Looper.java:123)
    07-28 08:58:58.081: W/System.err(1687):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    07-28 08:58:58.100: W/System.err(1687):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-28 08:58:58.100: W/System.err(1687):     at java.lang.reflect.Method.invoke(Method.java:521)
    07-28 08:58:58.100: W/System.err(1687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    07-28 08:58:58.100: W/System.err(1687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    07-28 08:58:58.100: W/System.err(1687):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

此代码

Intent f = new Intent(JHTTS.this, JHTTP.class);

是开始Activity,好像JHHTPActivity,但不是ThreadActivitystart()。您需要做的是Thread Intent而不是使用Intent

Thread Docs

  

这里有两种在新线程中执行代码的方法。您既可以继承Thread并重写其run()方法,也可以构造一个新的Thread并将Runnable传递给构造函数。在任何一种情况下,都必须调用start()方法来实际执行新的Thread。

而不是使用JHTTP jhttp = new JHTTP(); jhttp.start(); 尝试类似

的内容
{{1}}