android


Reading json from android internal storage error


This is my code of saving json:
public void saveCrimes (ArrayList<Crime> crimes) throws JSONException, FileNotFoundException, IOException {
JSONArray jsonArray = new JSONArray();
for (Crime c:crimes)
{
jsonArray.put(c.convertToJson());
}
Writer writer = null;
OutputStream out = context.openFileOutput(fileName, Context.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
writer.write(jsonArray.toString());
writer.close();
out.close();
}
And this is my code of reading json :
public ArrayList<Crime> loadCrimes () throws IOException, JSONException {
ArrayList<Crime> crimes = new ArrayList<Crime>();
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
String line = null;
try {
InputStream in = context.openFileInput(fileName);
reader = new BufferedReader(new InputStreamReader(in));
while((line = reader.readLine()) != null)
builder.append(line);
JSONArray array = (JSONArray)new JSONTokener(builder.toString()).nextValue();
for(int i = 0; i < array.length(); i++)
{
crimes.add(new Crime(array.getJSONObject(i)));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally {
if (reader != null)
reader.close();
}
return crimes;
}
The question is: code of saving json is correct, but when I run my application on my phone, create new crime, and restart the app, json stored before cannot be read. What makes me more confused is that when I run it on an emulator, it works well. Why?
I know what is wrong. Android Studio 2.0 cannot save the changes you have made in your codes if the change is very small. The solution is that building apk before running your application.

Related Links

Android emulator 3.0 kernel
Simple mediaPlayer example doesn't work! Why?
Android Bluetooth Device Locator
How to schedule Phone call event?
How to find a location on Map from a address which has been input by user?
Get the YouTube playlist and the video ID of a channel in Android
What is “ Stub ” and “AIDL” for in java?
Override android headset hook long press
License Screen About Phone - Legal Information - Open Licenses Screen [closed]
Titanium: Customization of Android Tab Group Bar and Tabs
Android: How to programatically create multiple imagebutton views?
Please suggest how to create a section in Android list
How to turn off “Use wireless networks” in Location and security in Android settings programatically?
Queue multiple entity modifiers in AndEngine
In-app purchases to unlock paid functions
preference screen doesn't honor NoTitleBar theme

Categories

HOME
asp.net-web-api
kentico
delphi-7
spark-streaming
string
c99
r-googlesheets
switch-statement
mips
pyqt4
jtextpane
crystal-lang
lanczos
modal-dialog
elasticsearch-5
mql5
openpyxl
text-mining
nancy
renjin
plink
scrolltop
docx
parameter-passing
linear-algebra
job-scheduling
easyphp
poedit
squashfs
imagemap
libigl
joptionpane
licensing
mat
vex
corenlp-server
python-2.6
laravel-eloquent
card.io
indy10
mnist
timing
odroid
gdata
railstutorial.org
visa
attributeerror
dbnull
contract
uiimage
openresty
linter
tinymce-3
convex-optimization
corruption
qtranslate
headless
template-toolkit
objectscript
jtree
node-request
hadoop-2.7.2
latin
flex4.6
wso2developerstudio
i386
expectations
android-dateutils
pre-build-event
mediametadataretriever
uiactionsheet
upx
pushbots
relativelayout
assertion
terracotta
flock
rdflib
entity-framework-4.3
xmltype
openbabel
solace-mq
sphinxql
scrypt
breakout
rolify
tlf
diem
video-codecs
server-administration
virtual-pc
alice
predicatebuilder
box2d-iphone
convention
gobject-introspection
user-tracking
winmain
update-statement
quotation-marks
ruby-1.8
instantiationexception
addchild
web-garden
cfwindow
lxr
filesystemobject
lalr
tui
mathematical-notation
sifr3
flvplayback

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App