diff --git a/cocos/network/HttpAsynConnection.h b/cocos/network/HttpAsynConnection.h index 1ebcae9ffb..21e7f5cd1e 100644 --- a/cocos/network/HttpAsynConnection.h +++ b/cocos/network/HttpAsynConnection.h @@ -46,6 +46,7 @@ @property (readonly) NSString *statusString; @property (strong) NSError *responseError; +@property (strong) NSError *connError; @property (strong) NSURLConnection *conn; diff --git a/cocos/network/HttpAsynConnection.m b/cocos/network/HttpAsynConnection.m index 1be9ff2fdd..a9177d7805 100755 --- a/cocos/network/HttpAsynConnection.m +++ b/cocos/network/HttpAsynConnection.m @@ -40,6 +40,7 @@ @synthesize responseCode; @synthesize statusString; @synthesize responseError; +@synthesize connError; @synthesize conn; @synthesize finish; @synthesize runLoop; @@ -65,7 +66,9 @@ self.responseData = [NSMutableData data]; getDataTime = 0; + self.responseError = nil; + self.connError = nil; // create the connection with the target request and this class as the delegate self.conn = [[[NSURLConnection alloc] initWithRequest:request @@ -109,10 +112,9 @@ */ if (responseCode < 200 || responseCode >= 300) {// something went wrong, abort the whole thing - - [connection cancel]; - finish = true; - return; + self.responseError = [NSError errorWithDomain:@"CCBackendDomain" + code:responseCode + userInfo:@{NSLocalizedDescriptionKey: @"Bad HTTP Response Code"}]; } [responseData setLength:0]; @@ -138,7 +140,7 @@ didFailWithError:(NSError *)error { //NSLog(@"Load failed with error %@", [error localizedDescription]); - self.responseError = error; + self.connError = error; finish = true; } diff --git a/cocos/network/HttpClient-apple.mm b/cocos/network/HttpClient-apple.mm index 883bb85a3d..34c9f64d0e 100644 --- a/cocos/network/HttpClient-apple.mm +++ b/cocos/network/HttpClient-apple.mm @@ -242,11 +242,18 @@ static int processTask(HttpRequest *request, NSString* requestType, void *stream } //if http connection return error + if (httpAsynConn.connError != nil) + { + NSString* errorString = [httpAsynConn.connError localizedDescription]; + strcpy(errorBuffer, [errorString UTF8String]); + return 0; + } + + //if http response got error, just log the error if (httpAsynConn.responseError != nil) { NSString* errorString = [httpAsynConn.responseError localizedDescription]; strcpy(errorBuffer, [errorString UTF8String]); - return 0; } *responseCode = httpAsynConn.responseCode; diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHttpURLConnection.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHttpURLConnection.java index 53b25e6100..c0d9c5f7c4 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHttpURLConnection.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHttpURLConnection.java @@ -260,8 +260,17 @@ public class Cocos2dxHttpURLConnection } static byte[] getResponseContent(HttpURLConnection http) { - try { - DataInputStream in = new DataInputStream(http.getInputStream()); + DataInputStream in; + try { + in = new DataInputStream(http.getInputStream()); + } catch (IOException e) { + in = new DataInputStream(http.getErrorStream()); + } catch (Exception e) { + Log.e("Cocos2dxHttpURLConnection exception", e.toString()); + return null; + } + + try { byte[] buffer = new byte[1024]; int size = 0; ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); @@ -272,12 +281,13 @@ public class Cocos2dxHttpURLConnection byte retbuffer[] = bytestream.toByteArray(); bytestream.close(); return retbuffer; - } catch (Exception e) { - Log.e("Cocos2dxHttpURLConnection exception", e.toString()); - } + } catch (Exception e) { + Log.e("Cocos2dxHttpURLConnection exception", e.toString()); + } + return null; } - + static int getResponseCode(HttpURLConnection http) { int code = 0; try {