wymsee / cordova-HTTP

Cordova / Phonegap plugin for communicating with HTTP servers. Allows for SSL pinning!
MIT License
371 stars 300 forks source link

Get Method may resulting in crash. #100

Open jwliang1226 opened 8 years ago

jwliang1226 commented 8 years ago

I get crash when I try to get the specific URL site in iOS, android without this issue.

For example:

cordovaHTTP.get("http://gb.85novel.com/book/2501.html",
{}, {},
function(response) {
    console.log(response.data);},
function(response) {
});

Following are the XCODE error messages:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* setObjectForKey: object cannot be nil (key: data)' * First throw call stack: ( 0 CoreFoundation 0x0000000113950d85 exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000011339bdeb objc_exception_throw + 48 2 CoreFoundation 0x0000000113848382 -[NSDictionaryM setObject:forKey:] + 1042 3 novel 0x000000010faea504 25-[CordovaHttpPlugin get:]_block_invoke + 196 4 novel 0x000000010faf0ca4 116-[AFHTTPSessionManager dataTaskWithHTTPMethod:URLString:parameters:uploadProgress:downloadProgress:success:failure:]_block_invoke.97 + 228 5 novel 0x000000010fb0ca19 72-[AFURLSessionManagerTaskDelegate URLSession:task:didCompleteWithError:]_block_invoke_2.150 + 201 6 libdispatch.dylib 0x0000000114ccfd9d _dispatch_call_block_and_release + 12 7 libdispatch.dylib 0x0000000114cf03eb _dispatch_client_callout + 8 8 libdispatch.dylib 0x0000000114cd81ef _dispatch_main_queue_callback_4CF + 1738 9 CoreFoundation 0x00000001138aa0f9 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE** + 9 10 CoreFoundation 0x000000011386bb99 CFRunLoopRun + 2073 11 CoreFoundation 0x000000011386b0f8 CFRunLoopRunSpecific + 488 12 GraphicsServices 0x000000011553aad2 GSEventRunModal + 161 13 UIKit 0x0000000111e1ff09 UIApplicationMain + 171 14 novel 0x000000010fad5d91 main + 65 15 libdyld.dylib 0x0000000114d2492d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

maurozac commented 8 years ago

I found the same issue.

To avoid the crash I add this wrap:

if(responseObject){ 
    [dictionary setObject:responseObject forKey:@"data"];
}

But it do not solve the real problem and I can't figure out what is happening. I got a response with status 200 and correct headers, but without 'data'.

In Android, with exactly the same setup (e.g. same request headers) it works as expected.

Someone could help?

jwliang1226 commented 8 years ago

Because the site includes Simplified Chinese characters, so I change NSUTF8StringEncoding to CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_ 18030_2000)

But it also do not solve all problem when encode method isn't GB1312 or GBK.

Someone has a better idea?

maurozac commented 8 years ago

Thank you! The crash is really caused due different string encode. As several pages still do not use UTF-8 as default encode, a workaround is adding a "second-chance decoder" at line 54 in TextResponseSerializer.m

if(!responseString){
    responseString = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
}
milesstewart88 commented 8 years ago

@Maurozac is there any chance of you forking this work around for those of us that use phonegap build?