Merge pull request #10340 from seobyeongky/network_bugfix2

receive content data even though status code is not 2xx
This commit is contained in:
minggo 2015-03-19 21:12:51 +08:00
commit 8e32933dd3
4 changed files with 32 additions and 12 deletions

View File

@ -46,6 +46,7 @@
@property (readonly) NSString *statusString; @property (readonly) NSString *statusString;
@property (strong) NSError *responseError; @property (strong) NSError *responseError;
@property (strong) NSError *connError;
@property (strong) NSURLConnection *conn; @property (strong) NSURLConnection *conn;

View File

@ -40,6 +40,7 @@
@synthesize responseCode; @synthesize responseCode;
@synthesize statusString; @synthesize statusString;
@synthesize responseError; @synthesize responseError;
@synthesize connError;
@synthesize conn; @synthesize conn;
@synthesize finish; @synthesize finish;
@synthesize runLoop; @synthesize runLoop;
@ -65,7 +66,9 @@
self.responseData = [NSMutableData data]; self.responseData = [NSMutableData data];
getDataTime = 0; getDataTime = 0;
self.responseError = nil; self.responseError = nil;
self.connError = nil;
// create the connection with the target request and this class as the delegate // create the connection with the target request and this class as the delegate
self.conn = [[[NSURLConnection alloc] initWithRequest:request self.conn = [[[NSURLConnection alloc] initWithRequest:request
@ -109,10 +112,9 @@
*/ */
if (responseCode < 200 || responseCode >= 300) if (responseCode < 200 || responseCode >= 300)
{// something went wrong, abort the whole thing {// something went wrong, abort the whole thing
self.responseError = [NSError errorWithDomain:@"CCBackendDomain"
[connection cancel]; code:responseCode
finish = true; userInfo:@{NSLocalizedDescriptionKey: @"Bad HTTP Response Code"}];
return;
} }
[responseData setLength:0]; [responseData setLength:0];
@ -138,7 +140,7 @@
didFailWithError:(NSError *)error didFailWithError:(NSError *)error
{ {
//NSLog(@"Load failed with error %@", [error localizedDescription]); //NSLog(@"Load failed with error %@", [error localizedDescription]);
self.responseError = error; self.connError = error;
finish = true; finish = true;
} }

View File

@ -242,11 +242,18 @@ static int processTask(HttpRequest *request, NSString* requestType, void *stream
} }
//if http connection return error //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) if (httpAsynConn.responseError != nil)
{ {
NSString* errorString = [httpAsynConn.responseError localizedDescription]; NSString* errorString = [httpAsynConn.responseError localizedDescription];
strcpy(errorBuffer, [errorString UTF8String]); strcpy(errorBuffer, [errorString UTF8String]);
return 0;
} }
*responseCode = httpAsynConn.responseCode; *responseCode = httpAsynConn.responseCode;

View File

@ -260,8 +260,17 @@ public class Cocos2dxHttpURLConnection
} }
static byte[] getResponseContent(HttpURLConnection http) { static byte[] getResponseContent(HttpURLConnection http) {
try { DataInputStream in;
DataInputStream in = new DataInputStream(http.getInputStream()); 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]; byte[] buffer = new byte[1024];
int size = 0; int size = 0;
ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
@ -272,12 +281,13 @@ public class Cocos2dxHttpURLConnection
byte retbuffer[] = bytestream.toByteArray(); byte retbuffer[] = bytestream.toByteArray();
bytestream.close(); bytestream.close();
return retbuffer; return retbuffer;
} catch (Exception e) { } catch (Exception e) {
Log.e("Cocos2dxHttpURLConnection exception", e.toString()); Log.e("Cocos2dxHttpURLConnection exception", e.toString());
} }
return null; return null;
} }
static int getResponseCode(HttpURLConnection http) { static int getResponseCode(HttpURLConnection http) {
int code = 0; int code = 0;
try { try {