有的时候后台返回的json数据太多,在控制台log日志打印时,一次性输出不全,网上有介绍使用log工具类,这里不再赘述。
我们用到另一种方法,把请求的得到的json数据写入txt文件:
String sdCardDir =Environment.getExternalStorageDirectory().getAbsolutePath();
File saveFile = new File(sdCardDir, "aaaa.txt");
FileOutputStream outStream = new FileOutputStream(saveFile);
outStream.write("json数据".getBytes());
outStream.close();
或者:
public static void getString(String str) {
String filePath = null;
boolean hasSDCard =Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (hasSDCard) {
filePath =Environment.getExternalStorageDirectory().toString() + File.separator +"hello.txt";
} else
filePath =Environment.getDownloadCacheDirectory().toString() + File.separator +"hello.txt";
try {
File file = new File(filePath);
if (!file.exists()) {
File dir = newFile(file.getParent());
dir.mkdirs();
file.createNewFile();
}
FileOutputStream outStream = newFileOutputStream(file);
outStream.write(str.getBytes());
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}