问题
I created a json file locally. I can view that in data>data>com.example.storage>files>filename.json. Using this, I want to fetch all the text values locally & download all images and save it in SD card from Image URL which is available in the json file. And in offline mode Itself I can load all the text values from json file as well as images from SD card.
Please let me know if this idea will work out or not. If not Please suggest some other way to do this.
The task is using local JSON file I want to fetch data. How to do that?(I am using separate ID for each data).
Sample JSON file is.
{"ImageData":[{"ImageId":"12","ImageName":"Img1","ImageDesc":"Img1 Img1Img1 ","ImageUrl":"img_url/pro1.jpg"},{"ImageId":"13","ImageName":"Img13","ImageDesc":"Img13 Img13 Img13Img13 ","ImageUrl":"img_url/pro2.jpg"},{"ImageId":"14","ImageName":"Img14","ImageDesc":"Img14 Img14 Img14 Img14 Img14 ","ImageUrl":"img_url/pro3.jpg.jpg"},]}
回答1:
Yes, it is possible. Next time provide some code so we have something to work with, there is not much people can do to solve this issue.
To get you started look at this tutorial about JSON: http://www.vogella.com/tutorials/AndroidJSON/article.html
This question will help you read a file locally: How can I read a text file in Android?
When you got the file as a string you can create a JSONObject
and parse the URLs and retrieve the images.
回答2:
Have you tried using the Gson library for Android ?
I'm using it and it works perfectly, you'll create objects that represent perfectly the Json. You then apply the Gson method on the Json and it will automatically fetch all the data in the object / objects that you created.
If you want to have a tutorial about how to use Gson, I think this guy explains really well. I suggest you to try this technic before asking anything else as it is a really commonly used library to parse Json Data. after using it, you'll have objects that reflect exactly the content of your Json File !
Hope it helped !
Oh btw, here is a website that will help you farmat your Json. I found it pretty useful myself, hope you'll like it ;)
回答3:
Well its quite simple! here it goes:
private void DoWork() {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
"file.json"); //path to your file assuming its in root of SD card
String content = "";
try {
BufferedReader br = new BufferedReader(new FileReader(file));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
content = sb.toString();
} finally {
br.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
mTextView.setText(""); // A multiline text view big enough
mTextView.append("\n\n-----START CONTENT-----\n\n");
mTextView.append(content);
mTextView.append("\n\n-----START JSON-----\n\n");
try {
JSONArray jsonArray = new JSONArray(content);
int length = jsonArray.length();
for (int i = 0; i < length; i++) { /itterate throu json array
JSONObject obj = jsonArray.getJSONObject(i);
//here you can access individual parts of you json object by getString()...getBoolean().... etc...
mTextView.append("\n" + obj.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
To read an internal file belonging to the app directory try:
this.openFileInput("yourfile.txt");
//`this` is the reference to you Activity or Context
Example:
FileInputStream in = openFileInput("filename.txt");
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
more details here
回答4:
here is code read JSON
data from file that file keep in asset folder in project
public JSONObject readDummyResponse() {
InputStream inputStream = null;
try {
inputStream = this.getAssets().open("sample_response.txt");
} catch (IOException e1) {
e1.printStackTrace();
}
JSONObject jsonObject = null;
if (inputStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
StringBuffer buffer = new StringBuffer();
String statement;
try {
while ((statement = reader.readLine()) != null) {
if (statement.trim().length() > 0) {
buffer.append(statement);
}
}
} catch (IOException e1) { // TODO Auto-generated catch block
e1.printStackTrace();
}
if (buffer.length() > 0) {
try {
jsonObject = new JSONObject(buffer.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return (JSONObject) jsonObject;
}
来源:https://stackoverflow.com/questions/24994878/how-to-fetch-data-from-local-json-file