Hi all,
I create a java web http server , and ios client send HTTP GET request to the web http server using NSURLConnection, then the server send back 200 OK response to the ios client. When I run my app on the iPhone simulator, everything is OK , my app can receive the response from the web http server for such progress:
1. My app call this callback function: -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
In this callback function, the "data" contains exactly the body of the 200 OK response packet.
2. My app then call this callback function: -(void)connectionDidFinishLoading:(NSURLConnection *)connection;
In this callback function, I think I have received all the data of response packet and do my job next...
But when I run my app on REAL Device(such as iPhone4s, iPad air, iPhone6), my app just call step 1 callback function: -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data, and not to call step 2, so I cannot do my job next.
There should be something wrong with my java web http server, but I do not know what is wrong. I have send the valid HTTP 200 OK packet:
Date now = new Date();
int len = 294;
String lenHeader = "Content-Length: " + len + "\r\n";
String dateHeader = "Date: " + now.toString() + "\r\n";
String message = "HTTP/1.1 200 OK\r\n"
+ "Content-Type: application/json\r\n"
+ "Server: Jerry Web Server\r\n"
+ "Connection: keep-alive\r\n"
+ lenHeader
+ dateHeader
+ "\r\n"
+ content; /*content is the body of the 200 OK packet*/
System.out.println("the message is \r\n " + message);
try {
output.write(message.getBytes());
output.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}