-
Notifications
You must be signed in to change notification settings - Fork 795
Description
We are using rn-fetch-blob to do all networking in our app, and are running into an issue on Android where sometimes JSON returned from our server is not properly decoded by rn-fetch-blob. Basically, Chinese, Japanese, and other non-ASCII characters get all messed up in the response object that is sent to our javascript code. It is still "valid" JSON, and all ascii characters are fine, but the non-ASCII characters are messed up.
Basically a response from the server that should look like this:
{ "name": "Chinese - 让教师及学生更为有效地操作" }
Is getting returned to us from rn-fetch-blob as this:
{ "name": "Chinese - 让æ��å¸�å��å¦ç��æ�´ä¸ºæ��æ��å�°æ��ä½�" }
Sometimes! This is important to note. Lots of non-ASCII text does seem to be fine and rn-fetch-blob behaves as expected. I am not 100% sure what characters cause the problem to manifest, but I am sure I know where the problem is after debugging rn-fetch-blob and have a good idea of what the fix might be.
The problem is this line:
https://github.com/joltup/rn-fetch-blob/blob/master/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java#L512
That line is taking the bytes of the response body and encoding them into utf8. Sometimes it throws a CharacterCodingException and then the catch body sends the bytes through some kind of base 64 encoding which I dont understand but basically sends the JSON back to the app without decoding it from utf8. Thus, the problem.
We can remove that line, and everything is ok. That is one possible fix.
However, I think what the code wants to be doing is decoding the bytes FROM utf8 (not encoding them INTO utf8, because the response body is already in utf8).
I could be totally off base here. But if I replace that line with:
Charset.forName("UTF-8").newDecoder().decode(ByteBuffer.wrap(b));
Everything works ok and I feel like that probably honors the intention of the code.
(I also think this is what react-native is doing, though also to be honest I dont understand the flow of their networking code well: https://github.com/facebook/react-native/blob/1151c096dab17e5d9a6ac05b61aacecd4305f3db/ReactAndroid/src/main/java/com/facebook/react/modules/network/ProgressiveStringDecoder.java#L67)
Anyways, I am happy to submit a PR if one of those things sounds like an appropriate fix, just let me know.
- RN 0.57.1
- rn-fetch-blob 0.10.13