0

I want to open a fragment using navigation component and deeplink.

val serializedUser = Gson().toJson(user)
findNavController().navigate(Uri.parse("myapp://?user=${serializedUser}"))

but in destination fragment sometimes argument is not complete.

val serializedUser = arguments?.getString("user")
val post = Gson().fromJson(serializedUser, User::class.java)

and getting this error:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 1156 path $.text

if I log serializedUser in destination fragment this not complete and some character in end of it is not exist, while in first fragment it is complete.

serialized model is a long string but seems length of it is not related for this problem. because some longer serialized model working.

this problem is occur only if using navigation component deeplink.

Hadi Ahmadi
  • 1,924
  • 2
  • 17
  • 38
  • 1
    The problem might be that `serializedUser` is not URL encoded before it is parsed; could you try using [`Uri.fromParts(...)`](https://developer.android.com/reference/android/net/Uri#fromParts(java.lang.String,%20java.lang.String,%20java.lang.String)) or [`Uri.Builder`](https://developer.android.com/reference/android/net/Uri.Builder) instead? – Marcono1234 Dec 16 '20 at 21:12
  • thank you @Marcono1234. you did great help to me. please write your comment in answer so that I can vote and mark it as accept and other can see your answer better. – Hadi Ahmadi Dec 17 '20 at 05:46

1 Answers1

1

Uri.parse(String) parses an encoded URI string, however you are not encoding serializedUser.

To solve this you should use Uri.fromParts(...) or the Uri.Builder class instead which does not require you to encode serializedUser.

Marcono1234
  • 5,856
  • 1
  • 25
  • 43