Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:Http响应-保存到文件UTF8编码_Android_Json_Encoding_Utf 8_Gson - Fatal编程技术网

Android:Http响应-保存到文件UTF8编码

Android:Http响应-保存到文件UTF8编码,android,json,encoding,utf-8,gson,Android,Json,Encoding,Utf 8,Gson,我读过很多文章,但实际上到目前为止,没有一篇有用。我正在尝试下载并解析包含一些UTF8字符的JSON。除了这些语法的编码之外,一切都很好。你能帮帮我吗 HttpGet request = new HttpGet(uri); HttpResponse resp = client.execute(request); entity = resp.getEntity(); fos = context.openFileOutput(fileSavePath, Context.MODE_PRIVATE);

我读过很多文章,但实际上到目前为止,没有一篇有用。我正在尝试下载并解析包含一些UTF8字符的JSON。除了这些语法的编码之外,一切都很好。你能帮帮我吗

HttpGet request = new HttpGet(uri);
HttpResponse resp = client.execute(request);
entity = resp.getEntity();
fos = context.openFileOutput(fileSavePath, Context.MODE_PRIVATE); //fos is FileOutputStream
entity.writeTo(fos);
entity.consumeContent();
fos.flush();
fos.close();
然后我像这样打开文件:

stream = context.openFileInput(AppConfig.CATEGORIES_JSON_PATH);
reader = new InputStreamReader(stream, StringUtils.UTF8);
我相信gson的东西是正确的,只是打开和保存文件的事情。我还认为错误在于实体。书面形式;,原因未指定任何编码。所以我尝试使用字节缓冲区和OutputStreamWriter写入,但gson抛出了格式错误的DJSONException:Unterminated string


非常感谢您的帮助

问题自行解决。虽然在查找sqlite数据库时,字符不正确,但应用程序中的所有内容看起来都很好。

接受的答案可能会有所帮助。openFileOutput不允许您指定编码,但您可以将生成的FileOutputSteam封装在OutputStreamWriter中,该OutputStreamWriter的构造函数采用编码。我在文章末尾写道,我已经尝试过了。没有成功您的帖子提到使用字节缓冲区和OutputStreamWriter,这与我建议的略有不同。您是否尝试过使用新的OutputStreamWriterfos、UTF-8并向该对象写入数据?是的,我尝试过。我还尝试将for包装到OutputStreamWriter中