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

Cordova. How to reproduce killing application in background mode?
How pivotY and pivotX are calculated in android <rotate>
Can't save or select the values to/from SQLITE
Retrofit2 issue with posting array
Is there a way to only extract resources with apktool?
How to prevent color fiter change of one ImageView, when changing other?
'User' has a primary key, use 'createObject(Class<E>, Object)' instead
Openfire remote-server-error(502)
Intent for protected apps in huawei
How to limit the max fontsize with sp in android
Navigation Drawer: setNavigationItemSelectedListener is not working.
Is it possible for an android app to report TWO fatal errors as it fails?
Mapsforge does not redraw cluster POI from Realm
How to refresh Android ListView after SyncAdapter completes a Sync
how to show both device and external storage in my listview
ContextMenu position is messed up in Android 7

Categories

HOME
gridview
spark-streaming
developer-tools
c++11
jax-rs
mysql-workbench
battery
jwplayer
composite-primary-key
cublas
switch-statement
telegram-bot
nested-if
jquery-ui
performancepoint
project
owin
fhir
resultset
comsol
red5
android-7.0-nougat
wolframalpha
html5-appcache
sigma.js
asp.net-core-webapi
windows-xp
drive
zxing
direct3d11
google-api-client
expressionengine3
tiki-wiki
8051
rpgle
winexe
uitapgesturerecognizer
cart
playframework-2.1
trigonometry
softmax
placeholder
optimizely
x++
jboss-4.2.x
robomongo
spring-data-couchbase
java-6
arules
libjpeg
cd-burning
rmongo
cronexpression
slackware
pure-css
vulcanize
jboss-tools
nullable
thread-sleep
cycle2
ascii-art
state-restoration
java-money
intel-c++
visual-sourcesafe-2005
wso2greg
sql-server-data-tools
avahi
jquery-forms-plugin
execve
packet-sniffers
viewer
strawberry-perl
gnumeric
mutual-authentication
radiant
rfc5322
c++builder-xe5
breakout
mongohq
observium
scringo
mvc-editor-templates
workitem
node-blade
traceability
robotlegs
extreme-programming
veracity
saleslogix
predicatebuilder
google-maps-mobile
user-tracking
svk
eclipse-marketplace
tabpanel
wysiwym
fdf
entitykey
leader

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