Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving data from HTTP Post using Sockets
    primarykey
    data
    text
    <p>my requirement is to receive HTTP Post request using Socket in android(Contents are JSON file). When I print socket.getInputStream(), I get:</p> <pre><code>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&lt;Data&gt; </code></pre> <p>I used the link <a href="https://forums.oracle.com/forums/thread.jspa?messageID=7076709" rel="nofollow">https://forums.oracle.com/forums/thread.jspa?messageID=7076709</a>, but it prints only "Content-type: application/json Host:10.0"(may be for length of 41) in Logcat. </p> <p>My code is:</p> <pre><code> serverSocket = new ServerSocket(portNumber); while(true){ socket = serverSocket.accept(); List&lt;String&gt; headers = new ArrayList&lt;String&gt;(); 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&lt;headers.size();j++) { Log.d("Headers:",headers.get(j)); } StringBuilder requestContent = new StringBuilder(); int ch; for (int i = 0; i &lt; contentLength; i++) { requestContent.append((char) reader.read()); } Log.d("The request content is: ",""+requestContent); </code></pre> <p>This is my output: Headers: POST / HTTP/1.1 Content-Length: 41</p> <p>requestContent: Content-Type: application/json Host: 10.</p> <p>Can someone please guide me how it can be done? Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload