
Mobile Web Server Exercise HTTP Web Server # This is a simple quick exercise. You will create a simple HTTP Server in your Android application using AndroidAsync (https://github.com/koush/AndroidAsync). # Create a new Project in Android Studio. Then, in your build.gradle, add implementation 'com.koushikdutta.async:androidasync:2.+' # For example, in your dependencies. Your dependencies may look like below: dependencies { implementation 'com.koushikdutta.async:androidasync:2.+' implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint- layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } # In your MianActivity class, add a new method as below: private void startServer(){ AsyncHttpServer server = new AsyncHttpServer(); server.get("/", new HttpServerRequestCallback() { @Override public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { response.send("Hello World!!!"); } }); // port 50000 server.listen(50000); } # This method will start the HTTP server with port 50000. Note when you paste the code, you need to import the classes. # Run your application then from the browser of the same device you run the app, type localhost:50000 in the address bar, you will get the simple response - 'Hello World!!!' If you are using a real device, you can also try opening the mobile's server from your laptops browser, you just need to know the IP of the server (e.g. look under Settings ->About Phone -> Status). Just make sure the device's are in the same network (easiest way to try is to create a WiFi hotspot with your phone) If you are using an emulator and want to access the mobile's HTTP server from your host machine (e.g. your laptop), check out: https://developer.android.com/studio/run/emulator- networking#redirection .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages2 Page
-
File Size-