How to Create a Http Server in Android

How to create a HTTP server in Android?

Consider this one:
https://github.com/NanoHttpd/nanohttpd.
Very small, written in Java. I used it without any problem.

I would like to create a Simple Http server in android

Try to use this library, it seems to respond to your needs:
AndroidAsync

How to create a simplest web server in android?

NanoHttpd is a full-fledged web server within a single java class. You'll be up and running in minutes.

How to implement an HTTP server on android

You may confuse two different level of networking communication.


Level 4 : TCP connection between two sockets.

A logical pipe between to side (may be single(two process?) or two different computers)
only connection handling data are handle at this level


Level 7 : Browser / Application Server used particular communication "language" to exchange high level data (file , images, audio ..) and is handled at this level.


Your question is about to open a Listening Socket (level 4) and a client that talk with it with a HTTP protocol (level 7).

So you're miss to fill the gap socket listening side to handle HTTP protocol
May be a java web server implementation may help you.

  • a Java EE way to have HTTP server : "Tiny Java Web Server" @ http://tjws.sourceforge.net/ that may help you for what you're looking for

  • an "Apache" way to have HTTP server http://www.androiddevblog.net/android/a-bare-minimum-web-server-for-android-platform


    see
    http://en.wikipedia.org/wiki/OSI_model#Layer_3:_Network_Layer
    to have more complete view of these Communication Layer

How to create HTTP server app in flutter?

It's possible without any third-party packages. Dart's io package provides functionalities to work with file, socket, http and other I/O related things. You can start listening for HTTP requests on a specific address and port using HttpServer.bind. Here is a snippet I found (link):

startServer() async {
var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
print("Server running on IP : " +
server.address.toString() +
" On Port : " +
server.port.toString());
await for (var request in server) {
request.response
..headers.contentType =
new ContentType("text", "plain", charset: "utf-8")
..write('Hello, world')
..close();
}
}

How to create a web server using android studio?

Im not sure if this is good idea if you are new to android. Maybe start with something easier but if you do plan to do it any way.

To send data collected by the app you will have to send it to some kind of backend you will have to make it i suggest making it in PHP and saving the data in a Mysql database.

For the sending part you will probably want to do it in the background with a AsyncTask and it will look something like this :

public class Backend extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
String server = "http://example.com/foo/bar"
String uid = params[0];
String name = params[1];
String email = params[2];

HttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(server+"NewUser.php");

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(1);
nameValuePair.add(new BasicNameValuePair("uid", uid));
nameValuePair.add(new BasicNameValuePair("email", email));
nameValuePair.add(new BasicNameValuePair("name", name));

try {
// Probeer dat te zetten
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

try {
// Execute
HttpResponse response = httpClient.execute(httpPost);
// Zet response om naar String
String responseBody = EntityUtils.toString(response.getEntity());
return responseBody;

} catch (ClientProtocolException e) {
e.printStackTrace();
return "0";
} catch (IOException e) {
e.printStackTrace();
return "0";
}
}

}

And you can execute this class like this:

new Backend().execute("1","maantje","email@example.com")

This will now POST uid, email, name to example.com/foo/bar/NewUser.php
So you will have to create that the data will be in $_POST['uid],$_POST['email'] and $_POST['name'].

Also don't forget the internet permissions in your manifest

<uses-permission android:name="android.permission.INTERNET" />

For the live feed option i have no experience with that but maybe this is a good place to start https://github.com/fyhertz/libstreaming

Can I run an HTTP server on a mobile platform?

Yes

  • Android examples: How to create a HTTP server in Android?.
  • iOS examples: iPhone HTTP Server.

For a full app that uses an http server, check out Websharing on Android.



Related Topics



Leave a reply



Submit