2

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"},]}
Nikhil Gangai
  • 125
  • 3
  • 7
user3879110
  • 17
  • 1
  • 7

4 Answers4

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.

Community
  • 1
  • 1
Rawa
  • 13,357
  • 6
  • 39
  • 55
1

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;
}
MilapTank
  • 9,988
  • 7
  • 38
  • 53
  • thanks for your reply. But I maintain json file in Internal memory. Please help for this. – user3879110 Jul 28 '14 at 12:27
  • than you can give path to `inputStream` Google `how to read file internal memory android` – MilapTank Jul 28 '14 at 12:28
  • @andruboy Sorry I think I edited your answer instead of my own :P please check that if I did not screw your answer. sorry! – Dumbo Jul 28 '14 at 12:52
  • @ andruboy, I implemented your code and I got error please clarify. (Please refer question again) – user3879110 Jul 29 '14 at 09:38
  • as i have seen your error its show that you have JSONArray and in my code i am developing JSONObject so you have to change code according to your code – MilapTank Jul 29 '14 at 09:54
0

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 ;)

Kevin Gilles
  • 463
  • 1
  • 8
  • 23
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

Dumbo
  • 13,555
  • 54
  • 184
  • 288