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 (strong) NSError *responseError;
@property (strong) NSError *connError;
@property (strong) NSURLConnection *conn;

View File

@ -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;
}

View File

@ -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;

View File

@ -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 {