1

my requirement is to receive HTTP Post request using Socket in android(Contents are JSON file). When I print socket.getInputStream(), I get:

POST/HTTP/1.1
Content length:41
Content-type: application/json
Host:10.0.2.15:5001
Connection: Keep Alive
User-Agent: Apache HTTPClient/Unavailable(Java 1.4)

CRLF(Carriage return line feed)-  

Actual Contents of JSON<Data>

I used the link https://forums.oracle.com/forums/thread.jspa?messageID=7076709, but it prints only "Content-type: application/json Host:10.0"(may be for length of 41) in Logcat.

My code is:

    serverSocket = new ServerSocket(portNumber);
    while(true){ 
    socket = serverSocket.accept();
    List<String> headers = new ArrayList<String>();
    String str;
    BufferedReader reader = new BufferedReader(new  InputStreamReader(socket.getInputStream()));
    while ((str = reader.readLine()) != null)
    {
    headers.add(str);
    if (str.startsWith("Content-Length: "))
    {
    break; // and don't get the next line!
    }
    }
    int contentLength = Integer.parseInt(headers.get(headers.size() - 1).substring("Content-Length: ".length()));

   for(int j=0;j<headers.size();j++)
   {
    Log.d("Headers:",headers.get(j));
    }
    StringBuilder requestContent = new StringBuilder();
    int ch;
    for (int i = 0; i < contentLength; i++)
    {
    requestContent.append((char) reader.read());
    }
    Log.d("The request content is: ",""+requestContent);

This is my output: Headers: POST / HTTP/1.1 Content-Length: 41

requestContent: Content-Type: application/json Host: 10.

Can someone please guide me how it can be done? Thanks in advance!

user1741274
  • 759
  • 3
  • 13
  • 25
  • Ya i ve edited the question @Waqas – user1741274 Oct 24 '12 at 10:14
  • 1
    Why are you using sockets instead of an http library? – artbristol Oct 24 '12 at 10:17
  • Please let me know if this scenario is possible(as my requirement is to implement so). Also I need some guidance about which HTTP library to use? Will it be light-weight? @artbristol – user1741274 Oct 24 '12 at 10:55
  • @user1741274 Have a look at the answers to this question http://stackoverflow.com/questions/6329468/create-http-server-android – artbristol Oct 24 '12 at 11:35
  • @artbristol . The asker is using sockets because he wants Android to be the http server. If it's possible to use http libraries for that, I'd like to know. – Arvin Jul 29 '13 at 08:33
  • 1
    @Arvin there seem to be a few in the link I provided (I've never used one personally) – artbristol Jul 29 '13 at 09:04

1 Answers1

2
DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
conn.bind(serverSocket.accept(), new BasicHttpParams());
HttpRequest request = conn.receiveRequestHeader();
conn.receiveRequestEntity((HttpEntityEnclosingRequest)request);
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
System.out.println(EntityUtils.toString(entity));

http://hc.apache.org/httpcomponents-core-ga/tutorial/pdf/httpcore-tutorial.pdf for more reference

user1741274
  • 759
  • 3
  • 13
  • 25