Reputation: 2063
I am reading a json file from a local drive and making it as gson. And it's working as a java application.
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader("D:\\AircraftService.json"));
DataObject[] obj = gson.fromJson(br, DataObject[].class);
System.out.println("The object length is: " + obj.length);
for(int i = 0; i<obj.length ; i++ )
{
System.out.println(obj[i].AC + " " + obj[i].Fleet + " " + obj[i].SubFleet);
}
Using this I am trying to populate an android list
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ListView lv= (ListView)findViewById(R.id.list);
Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader("D:\\AircraftService.json"));
DataObject[] obj = gson.fromJson(br, DataObject[].class);
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i=0 ; i<obj.length ; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("AC", obj[i].AC);
map.put("Fleet", obj[i].Fleet );
map.put("SubFleet", obj[i].SubFleet );
fillMaps.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.main, new String[] {"AC", "Fleet"}, new int[] {R.id.ac, R.id.fleet});
lv.setAdapter(adapter);
}
It's not able to populate the obj. Ouput is blank. Can you tell me where I am going wrong.
Upvotes: 0
Views: 166
Reputation:
I'm sure your file does not reside in D:\AircraftService.json
on your android device.
Adjust the path, use Environment.getExternalStorageDirectory()
to get the path to your SD-card or flash-memory if you stored your file there. If you stored it inside your apps asset folder, use file:///android_asset/
as your base path and append the file name (and subfolders).
Edit: Considering your comment down below, here's a short howto.
First of all I recommend storing the file in your assets, since reading the sdcard requires a seperate permission for your app.
Theres a folder in your project thats called res/
. Create a subfolder into that, name it raw
. Thats a standard android naming convention for storing files that are not standard resources (like layouts or strings). After that you should be able to access the file with the path file:///android_asset/raw/AircraftService.json
Upvotes: 1
Reputation: 50578
The path of the json file cannot be opened. Try moving the file lets say in assets folder
than open it.
Upvotes: 0