0

hello all i am creating a web server for android with NanoHttpd and when i run it .It says that the Activity has stopped working please help what should i do. This is the code which i am using. Here goes the Code:

    package dolphin.developers.com;

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

public class  MyHTTPD  extends NanoHTTPD {

    /**
    * Constructs an HTTP server on given port.
    */
   public MyHTTPD()throws IOException {
       super(8080);
   }


@Override
   public Response serve( String uri, Method method,
           Map<String, String> header, Map<String, String> parms,
           Map<String, String> files )
   {
       System.out.println( method + " '222" + uri + "' " );
       String msg = "<html><body><h1>Hello server</h1>\n";
       if ( parms.get("username") == null )
           msg +=
               "<form action='?' method='get'>\n" +
               "  <p>Your name: <input type='text' name='username'></p>\n" +
               "</form>\n";
       else
           msg += "<p>Hello, " + parms.get("username") + "!</p>";

       msg += "</body></html>\n";
       return new NanoHTTPD.Response(msg );
   }


   public static void main( String[] args )
   {
       try
       {
           new MyHTTPD();
       }
       catch( IOException ioe )
       {
           System.err.println( "Couldn't start server:\n" + ioe );
           System.exit( -1 );
       }
       System.out.println( "Listening on port 8080. Hit Enter to stop.\n" );
       try { System.in.read(); } catch( Throwable t ) {
           System.out.println("read error");
       };
   }

}

LogCat:

07-14 22:35:26.394: E/AndroidRuntime(581): FATAL EXCEPTION: main
07-14 22:35:26.394: E/AndroidRuntime(581): android.content.ActivityNotFoundException: Unable to find explicit activity class {dolphin.devlopers.com/dolphin.developers.com.MyHTTPD}; have you declared this activity in your AndroidManifest.xml?
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Activity.startActivityForResult(Activity.java:2817)
07-14 22:35:26.394: E/AndroidRuntime(581):  at android.app.Activity.startActivity(Activity.java:2923)
07-14 22:35:26.394: E/AndroidRuntime(581):  at dolphin.developers.com.facebook1$DownloadFileFromURL.onPostExecute(facebook1.java:167)
Prakhar
  • 2,270
  • 19
  • 26

1 Answers1

0

Look in your AndroidManifest.xml and make sure that your main activity is registered (in your case MyHTTPD). Heres a very basic manifest file, take a look at the tag. I put in your class as an entry.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.epstest"
    android:versionCode="1"
    android:versionName="1.0" >
    ....
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

--------------------------------------------------------------------------------------------------

        <activity
            android:name="dolphin.developers.com.MyHTTPD"
            android:label="@string/title"
            android:configChanges="orientation" >
        </activity>

--------------------------------------------------------------------------------------------------

    </application>

</manifest>
ug_
  • 11,267
  • 2
  • 35
  • 52
  • i added it but now it does not give me a error but when i try to view the server using 127.0.0.1:8080 it says can not connect to server anyway thanx for answereing.. – Prakhar Jul 17 '13 at 06:02
  • Id say 1.) make sure your webserver is running even after you close out of the application to look at it(if you look at it from your phone/tablet). 2.) To test this and test in general you may have better luck linking up your phone/tablet to your local wifi network and finding the IP address of your phone, from there use its local IP to access your server from your computer eg: 192.168.2.13:8080. Im just taking shots in the dark at this point :D good luck. – ug_ Jul 17 '13 at 06:07
  • when i tried that it says the host is taking too time to respond any suggestion.. – Prakhar Jul 17 '13 at 06:11
  • Only Idea I got is to use a different port than 8080. 8080 is a port used by tomcat servers and may be reserved on android phones (just quick guess). Just keep breakpoints set in your code and test regularly, try using debugging methods like pinging your phone via command prompt and when you encouter a problem start at what you can get to work and what you know is working, then work your way to the problem. GL – ug_ Jul 17 '13 at 06:15