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.
我在本地创建了一个json文件。我可以在data>data>com.example.storage>files>filename.json中查看。使用这个,我想要在本地获取所有的文本值并下载所有的图像,并将其保存在SD卡中,从json文件中提供的图像URL中获取。在离线模式下,我可以从json文件和SD卡上加载所有的文本值。
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).
任务使用的是我想要获取数据的本地JSON文件。如何做呢?(我对每个数据使用单独的ID)。
Sample JSON file is.
示例JSON文件。
{"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"},]}
4 个解决方案
#1
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
为了让您开始阅读这个关于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?
这个问题将帮助您在本地读取文件:如何在Android中读取文本文件?
When you got the file as a string you can create a JSONObject
and parse the URLs and retrieve the images.
当您将文件作为字符串获取时,您可以创建一个JSONObject并解析url并检索图像。
#2
0
Have you tried using the Gson library for Android ?
你试过在Android上使用Gson库吗?
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.
我正在使用它,它很好地工作,你会创建一个完全代表Json的对象。然后在Json上应用Gson方法,它将自动获取您创建的对象/对象中的所有数据。
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 !
如果你想要一个关于如何使用Gson的教程,我觉得这个人解释得很好。我建议您在询问其他内容之前尝试一下这个技术,因为它是一个真正常用的解析Json数据的库。在使用它之后,您将拥有完全反映Json文件内容的对象!
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 ;)
顺便说一句,这里有一个网站可以帮助你实现Json。我发现它很有用,希望你会喜欢它;
#3
0
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
0
here is code read JSON
data from file that file keep in asset folder in project
这是从项目中的资产文件夹中保存的文件中读取JSON数据的代码
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;
}
#1
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
为了让您开始阅读这个关于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?
这个问题将帮助您在本地读取文件:如何在Android中读取文本文件?
When you got the file as a string you can create a JSONObject
and parse the URLs and retrieve the images.
当您将文件作为字符串获取时,您可以创建一个JSONObject并解析url并检索图像。
#2
0
Have you tried using the Gson library for Android ?
你试过在Android上使用Gson库吗?
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.
我正在使用它,它很好地工作,你会创建一个完全代表Json的对象。然后在Json上应用Gson方法,它将自动获取您创建的对象/对象中的所有数据。
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 !
如果你想要一个关于如何使用Gson的教程,我觉得这个人解释得很好。我建议您在询问其他内容之前尝试一下这个技术,因为它是一个真正常用的解析Json数据的库。在使用它之后,您将拥有完全反映Json文件内容的对象!
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 ;)
顺便说一句,这里有一个网站可以帮助你实现Json。我发现它很有用,希望你会喜欢它;
#3
0
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
0
here is code read JSON
data from file that file keep in asset folder in project
这是从项目中的资产文件夹中保存的文件中读取JSON数据的代码
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;
}