0

I am trying to show "brand_name" and "description" from the below json data. Actually that json data file is under res/raw/brand.txt okay. Infact my logcat is not showing the printed values too...

Suggestions please

Thanks for your precious time!..

Plz find my source here

protected void get_brands_data() {      
    InputStream inputStream = getResources().openRawResource(R.raw.brand);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int prdct;
    try {
        prdct = inputStream.read();
        while (prdct != -1) {
            byteArrayOutputStream.write(prdct);
            prdct = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        // Parse the data into jsonobject to get original data in form of json.         
        JSONObject jObject = new JSONObject(byteArrayOutputStream.toString());
        JSONObject jObjectResult = jObject.getJSONObject("Products");

        String br_name = "";
        String br_desc = "";
        System.out.println("------" br_name);
        System.out.println("------" br_desc);
        t1.setText(br_name);
        t2.setText(br_desc);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

JSON DATA

{
"PublicationID": 53,
"PublicationDate": "18/01/2013",
"Publicationbackgroundimage": null,
"Products": [
    {
        "SequenceKey": 1,
        "ProductID": 100630,
        "Brand_Name": "Lindauer",
        "Region": "",
        "Country_of_Origin": "New Zealand",
        "Description": "Lindauer, the country's most popular sparkling wine brand, started life as a bold statement about the quality of wine that can be created in New Zealand's cool-climate. Made from traditional champagne grape varieties, Chardonnay and Pinot Noir and more recently premium Marlborough Sauvignon Blanc grapes, it uses the authentic method of bottling the wine for its second fermentation, a technique that creates the sparkle and distinctive yeasty flavours, regularly outperforming more expensive wines. Wines in this range include Lindauer Brut NV, Lindauer Fraise, Lindauer Rose, Lindauer Sec and Lindauer Sauvignon Blanc."
    }
  ]
}
vilpe89
  • 4,656
  • 1
  • 29
  • 36
Nakshatran
  • 426
  • 1
  • 7
  • 19

1 Answers1

1

I would replace your "resource reader" with this:

String json = "";
try {
    BufferedInputStream resourceStream = new BufferedInputStream(getResources().openRawResource(R.raw.brand));
    BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream));

    String line;
    while((line = reader.readLine()) != null) {
        json += line;
    }
    reader.close();
    resourceStream.close();
}
catch(Exception ex) {
    Log.e("myApp", ex.getMessage());
}

and then:

JSONObject jObject = new JSONObject(json);
vilpe89
  • 4,656
  • 1
  • 29
  • 36