Reputation: 1091
I am trying to read json file from asstes folder android but i am not able to read it and even my logs are not getting displayed after read line can anyone tell me whats wrong ? here is my code: public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
loadJSONFromAsset();
}
public String loadJSONFromAsset() {
String json = null;
try {
Log.d("entry", "loadJSONFromAsset: "+json);
InputStream is = getAssets().open("data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
Log.d("json", "loadJSONFromAsset: "+json);
} catch (IOException ex) {
Log.d("error", "loadJSONFromAsset: "+ex.toString());
ex.printStackTrace();
return null;
}
return json;
}
here is the log:
07-10 22:38:53.174 9783-9783/com.nodapp D/entry: loadJSONFromAsset: null
}
07-10 22:38:53.174 9783-9783/com.nodapp I/ViewRootImpl: CPU Rendering VSync enable = true
07-10 22:38:53.174 9783-9783/com.nodapp W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
07-10 22:38:53.204 9783-9783/com.nodapp W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
07-10 22:38:53.234 9783-9783/com.nodapp W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
07-10 22:38:53.234 9783-9783/com.nodapp I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d3ecf3 time:24068058
07-10 22:38:53.314 9783-9783/com.nodapp W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
Upvotes: 0
Views: 984
Reputation: 241
Try this:
public String loadJSONFromAsset() {
String json = null;
try {
Log.d("entry", "loadJSONFromAsset: "+json);
InputStream is = getAssets().open("data.json");
StringBuilder buf=new StringBuilder();
BufferedReader in=
new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str;
while ((str=in.readLine()) != null) {
buf.append(str);
}
in.close();
json = buf.toString();
Log.d("json", "loadJSONFromAsset: "+json);
} catch (IOException ex) {
Log.d("error", "loadJSONFromAsset: "+ex.toString());
ex.printStackTrace();
return null;
}
return json;
}
Upvotes: 1