3

Is there an easy way to query german letters like 'ä', 'ö' 'ü' in an API with Darts http - API (in Flutter)?

An API query should download a jSON string to a Flutterapp. Unfortunately, the jSON string contains German special characters.

String einleseURL = 'http://xxx.xxx.xxx.xxx/einlesen.json';
final ausgeleseneJsonString = await http.get(Uri.encodeFull(einleseURL));
uiUeberprueft = serializers.deserializeWith(
  RenderEbeneErste.serializer, json.decode(ausgeleseneJsonString.body));

The only solution I can imagine is to convert the jSON String in utf-8's numeric values and save this new file at the server for http query. The list of numbers is then called by Flutter and decrypted with utf.decoode () before a json.decode () occurs.

please refer: How can I convert string to utf8 in Dart?

Is there a simpler way?

Markus Bach
  • 763
  • 1
  • 8
  • 24
  • There is nothing special about german special characters in regard to an HTTP request. What is the actual result you get? – Günter Zöchbauer Feb 18 '19 at 13:25
  • Dear Günter, thank you for response. The actual result is for examle: ' "nutzerHilfe": "Beschreibung hinzufügen", '. Also the generated UI in Flutter look exactly like the output debugPrint (ausgeleseneJsonString.body) on the console. – Markus Bach Feb 18 '19 at 13:44
  • What do you get if you load the URL in the browser? Perhaps the `charset` part in the `content-type` header sent by the server is not correct. Perhaps the string is not even UTF-8 encoded but some other encoding? – Günter Zöchbauer Feb 18 '19 at 13:46
  • The query runs via IIS on a computer. The .json file is encoded as utf-8 in Notepad ++. In the browser, the string is displayed correctly, even on a different device. – Markus Bach Feb 18 '19 at 14:01
  • What is the charset header? – Günter Zöchbauer Feb 18 '19 at 14:01
  • Honestly, I do not know where to read this. Btw. If I enter the jSON file of the app directly as 'asset' and thus bypass the API request, it works fine. – Markus Bach Feb 18 '19 at 14:39
  • If you open the network tab in Chrome devtools before you make the request you should be able to see the headers. – Günter Zöchbauer Feb 18 '19 at 14:41
  • Try https://stackoverflow.com/questions/51368663/flutter-fetched-japanese-character-from-server-decoded-wrong – Günter Zöchbauer Feb 18 '19 at 14:41
  • 1
    Many Thanks. With slightly modified source code from the linked page it runs. Thank you! If anyone is interested in the source code, I like to post. – Markus Bach Feb 18 '19 at 15:23
  • Yes please answer your question with the code that worked for you – Günter Zöchbauer Feb 18 '19 at 15:24

1 Answers1

13

I used the build_value serializer from Dart. Therefore, an object is created from the jSON string if json.decode () and serialization was successful.

The problem of the special characters was solved with the last line in this code.

Thanks again!

//Objekte festlegen
RenderEbeneErste uiUeberprueft;

//jSON auslesen, prüfen & Objekt erstellen
String jsonURL = 'http://xxx.xxx.xxx.xxx/eingeleseneJSON.json';
final ausgeleseneDaten = await http.get(Uri.parse(jsonURL));
uiUeberprueft = serializers.deserializeWith(
  RenderEbeneErste.serializer, json.decode(utf8.decode(ausgeleseneDaten.bodyBytes)));
Max
  • 11
  • 4
Markus Bach
  • 763
  • 1
  • 8
  • 24