How to implement an HTTP server on android
Asked Answered
S

4

9

I've two android applications on same LAN provided by WIFI :

  1. App (A) that open a listening socket on port 8033
  2. App (B) that use HttpClient to access (A) on port 8033

How to make it possible that (A) may do POST and GET requests on (B)?
What the URL used by (A) to access (B) looks like ?

Thanks to All.

Showthrough answered 1/9, 2011 at 9:18 Comment(1)
@Jonas His question is not whether a client can talk to a server but rather whether HttpClient can communicate to a server socket that his other app is listening to.Schear
E
13

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.

Evan answered 1/9, 2011 at 9:46 Comment(4)
Ok thank. But where can i find working example of Android & TJWS. I looked at the URL but I'm a complete noob in java so If i get some code then It'll be very easy for me to understand it.Showthrough
Tiny Java Web Server implement the standard servlet container defined by JEE (java Enterprise Edition): So my advices is 1)looks for "how to implement third party library/jar in android project" 2)learn a bit more of java Standard Edition (the classic ) and Enterprise Edition (used typicaly for java for application server) see as example java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html .. see as sample apl.jhu.edu/~hall/java/Servlet-Tutorial/…Evan
I came across this link "androiddevblog.net/android/…" which is doing what i think exactly what i want. Will you agree?Showthrough
I guess it is a good way to make lightweight http server. You should make a try with it. I update my answer accordingly .Good luckEvan
K
2

Check out AndServer project available on github. Its a Web server and Web framework of Android platform. It provides annotations like SpringMVC.

@RestController
@RequestMapping(path = "/user")
public class UserController {

    @PostMapping("/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password) {

        ...
        return "Successful.";
    }

    @GetMapping(path = "/{userId}")
    public User info(@PathVariable("userId") String userId,
                     @QueryParam("fields") String fields) {

        User user = findUserById(userId, fields);
        ...

        return user;
    }

    @PutMapping(path = "/{userId}")
    public void modify(@PathVariable("userId") String userId
                       @RequestParam("age") int age) {
        ...
    }
}

Simple deployment

Server server = AndServer.webServer(context)
    .port(8080)
    .timeout(10, TimeUnit.SECONDS)
    .build();

// startup the server.
server.startup();

...

// shutdown the server.
server.shutdown();
Kenley answered 4/12, 2021 at 10:39 Comment(0)
S
1

Listening on a port and accepting socket connections isn't enough to serve data back to HttpClient. Sockets in effect provide a physical pipe but know nothing about the format of the data that's flowing along that pipe. If you are set on using HttpClient, then you'll need to have your server application understand HTTP protocol (or at least a very basic subset of it).

If all you need is to have two processes communicate in some way, you may be better off having your server app be a service and then your client app interrogate this service for the required data.

Schear answered 1/9, 2011 at 9:23 Comment(5)
How can i make Server socket understand HTTP Protocol and sorry I forgot to write Client will be on other android device & Server will on the different one.Showthrough
"physical pipe" may be a restrictive point of view ... on a single device you may have a listening socket and a client. without any physical linkEvan
I have found many examples of HTTPClient that queries webserver & normal Server & Client Socket communication in java but I couldn't find any example on Server socket example that implements HTTP-Protocols & such.Showthrough
You won't find many example of server socket understanding an HTTP protocol, because it's quite complicated. About 12-13 years ago, I had to implement HTTP server in java - just basic HTTP request parsing took over 1000 lines of code. Unless you understand HTTP protocol well (and judging from your question you do not), I would strongly discourage you from doing so. Finally, getting one device talk to another via socket is, in my view, a bit strange approach.Schear
You are right i don't know much about protocol and the main reason i asked this question because the problem i'm facing is creating a WebServer in Android I have looked at iJetty and TJWS but as I'm completely noob in java i wasn't able to understand it so I thought If could make 2 different things communicate each other i doubted it'll work that's why I said it in my question that "it might be stupid". Thanks for your help.Showthrough
A
1

I was looking for the same solution and found NanoHTTPD - an extremely simple HTTP request handling server class. You can find the source and very easy to understand samples at https://github.com/NanoHttpd

Amador answered 3/12, 2014 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.