Android Question Loading JSON file from a website & Parsing JSON data of Multiple Arrays

Shahid Saeed

Active Member
Licensed User
Longtime User
I went through all the Tutorials about usage of JSON file, and currently found only few methods, Local JSON file and remote JSON file.

My Question is the method for remote JSON files which provides a way through connecting to MySQL server and fetch the data and parse into JSON. But what if someone doesn't have access over to MySQL and the only source he have is the JSON file link e.g (http://www.somewebsite.com/JSON/JSON.php). What will be the method to include that JSON file, or if there is a way to read data from remote file and store into Assets folder as local JSON file.

My Second Question is about Parsing specific JSON Data from Multiple Arrays

for example :

B4X:
{
   "news_sections": [
      {
         "section_title": "الأخبار",
         "news": [
            {
               "news_date": "الأربعاء ، ١٩ مارس  ٢٠١٤",
               "news_title": "الوارية تنتزع سيف صاحب السمو رئيس الدولة",
               "news_summary": "أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله،",
               "news": "<p align=\"right\">أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله، مؤكداً أن فوز هجن الرئاسة بـ «ناموس القائد»، يمثل أعلى وأروع تكريم لمسيرة إنجازاتها المتلاحقة، التي توجتها أمس بتتويج مستحق واستثنائي، مشيراً إلى أن ما شهده المهرجان الختامي بالوثبة، يمثل شاهداً حياً على دعم وعطاء القيادة للرياضات التراثية، وفي مقدمتها سباقات الهجن التي تعيش عصرها الذهبي. <\/p><p align=\"right\">كما أهدى معاليه إنجاز «هجن الرئاسة» إلى الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة، مشيداً بدعم سموه المتواصل للرياضة والرياضيين، وللرياضات التراثية بصفة خاصة، وفي مقدمتها سباقات الهجن، التي يحفظ منتسبوها لسموه أياديه البيضاء على هذه الرياضة، والتي تمثلت في عدة سباقات تطوف إمارات الدولة باسم سموه، إضافة إلى الدعم الدائم والسخي للسباقات في كل الميادين، ما جعل الإمارات وجهة للملاك والمضمرين من دول الخليج كافة. <\/p><p align=\"right\">وثمن معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، دعم وعطاء سمو الشيخ منصور بن زايد آل نهيان، نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مؤكداً أن عطاء سموه بلا حدود، وأن ما شهدته سباقات الهجن ومختلف الرياضات التراثية من تطور، كان نتاجاً لأفكار سموه الوثابة التي لا تحدها حدود، وتنطلق من حب عميق للوطن، ومن ربط رائع بين الماضي بموروثه العريق والحاضر بمعطيات التطور، تطلعاً إلى مستقبل حافل بإذن الله لدولتنا الناهضة. <br><\/p>",
               "image": "http://www.prcamels.ae/images/news/npic18711720.jpg"
            },
            {
               "news_date": "الأحد ، ١٦ مارس  ٢٠١٤",
               "news_title": "الخفيفة لـ «هجن الرئاسة» تتصدر منافسات الشيوخ للثنايا",
               "news_summary": "شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي",
               "news": "<div style=\"text-align: right;\">شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي، والتي خصصت لسن (الثنايا) الطفح ولمسافة 6 كلم، كما شهد المنافسات، جمهور غفير من محبي رياضة سباقات الهجن من أبناء الدولة وإخوانهم من دول مجلس التعاون الخليجي.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وتصدرت (الخفيفة) لهجن الرئاسة مع المضمر مبارك أحمد مطر الخييلي الشوط الأول والمخصص للبكار المحليات وأحرزت المركز الأول والناموس بعد أن قطعت مسافة الشوط في توقيت (9:15:2 دقيقة)، وجاءت في المركز الثاني (احتلال) لهجن الرئاسة والمضمر عيسى أحمد مطر الخييلي بتوقيت وقدره(9:17:2)، وحلت ثالثة (الوجبة) لهجن الشحانية والمضمر سالم فاران المري بتوقيت (9:20:00).<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic27449365.jpg"
            },
            {
               "news_date": "السبت ، ١٥ مارس  ٢٠١٤",
               "news_title": "هجن الرئاسة.. «خط أحمر»",
               "news_summary": "شهد سمو الشيخ حمدان بن محمد بن راشد آل مكتوم ولي عهد دبي، منافسات اليوم الخامس للمهرجان الختامي السنوي لسباقات الهجن في منطقة الوثبة بالعاصمة أبوظبي",
               "news": "<div style=\"text-align: right;\">شهد سمو الشيخ حمدان بن محمد بن راشد آل مكتوم ولي عهد دبي، منافسات اليوم الخامس للمهرجان الختامي السنوي لسباقات الهجن في منطقة الوثبة بالعاصمة أبوظبي، كما شهد المنافسات معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، والشيخ راشد بن حمدان بن محمد آل نهيان، والشيخ خليفة بن سيف بن محمد آل نهيان، والشيخ المر بن مكتوم آل مكتوم، وجمع غفير من محبي رياضة سباقات الهجن من أبناء الدولة وإخوانهم أبناء دول مجلس التعاون الخليجي.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وكانت هجن الرئاسة قد حلقت كعادتها بناموس الإبداع وأثبتت أنها «خط أحمر» على صعيد المنافسة، بعد أن اكتسحت معظم المراكز، وغردت وحدها خارج السرب، في مكانتها التي لا تدانيها فيها غيرها، وليس أدل على ذلك من حصولها على «هاتريك» في الأشواط الرئيسية لمنافسات «الإيذاع»، التي أقيمت لمسافة 6 كلم في الميدان الجنوبي للفترة المسائية، والتي شهدت تصدر (كيف) لهجن الرئاسة والمضمر راشد محمد بالسم المنصوري منافسات الشوط الأول «أبكار مفتوح» وفازت بالمركز الأول بعد أن تصدرت المراحل الأخيرة وحققت أفضل توقيت لفئة الإيذاع بعد أن قطعت المسافة بتوقيت وقدره (8:48:5)، وجاءت في المركز الثاني (كوره) لهجن الرئاسة والمضمر راشد محمد بالسم المنصوري وقطعت المسافة بتوقيت وقدره (8:49:8)، وفي المركز الثالث (هدوء) لهجن العاصفة والمضمر حمد راشد غدير الكتبي بتوقيت وقدره (8:56:1).<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic77497797.jpg"
            }
         ]
      },
      {
         "section_title": "الأخبار العامة",
         "news": [
            {
               "news_date": "الخميس ، ٣٠ يناير  ٢٠١٤",
               "news_title": "سلطان بن حمدان: دعم القيادة الرشيدة وراء تفوق هجن الإمارات",
               "news_summary": "هنأ معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة رئيس اتحاد سباقات الهجن، صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله، بنجاح العملية الجراحية داعيا الله عز وجل أن يمن على سموه بالصحة والعافية، كما هنأ معاليه صاحب السمو رئيس الدولة بإنجاز هجن الرئاسة بفوز «سمحة» بالسيف الذهبي للحول و«معزز» بالخنجر الذهبي للزمول المحليات.",
               "news": "<div style=\"text-align: right;\">هنأ معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة رئيس اتحاد سباقات الهجن، صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة - حفظه الله - بنجاح العملية الجراحية داعيا الله عز وجل أن يمن على سموه بالصحة والعافية، كما هنأ معاليه صاحب السمو رئيس الدولة بإنجاز هجن الرئاسة بفوز «سمحة» بالسيف الذهبي للحول و«معزز» بالخنجر الذهبي للزمول المحليات في مهرجان أمير قطر للهجن العربية الأصيلة، وهنأ معاليه الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة بالإنجاز المميز، كما هنأ معاليه سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة بتفوق هجن الرئاسة في ميدان الشحانية، وتوجه معاليه بالتهنئة إلى شعب الإمارات بهذا الإنجاز الكبير.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">&nbsp;وقال معاليه: «هذه الإنجازات هي نتاج للدعم غير المحدود الذي تجده رياضات الآباء والأجداد عامة وسباقات الهجن على وجه الخصوص، من سموه التي تهدف إلى تعزيز قيم الماضي في نفوس الأبناء والأجيال المقبلة، مشيراً معاليه إلى أن هذا الدعم السخي كفل لهجن الرئاسة تحقيق الانتصارات والتتويج بأرفع الألقاب في السباقات التي تشارك فيها داخل وخارج الدولة. وأكد معاليه أن الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة يقدم الدعم والاهتمام الكبيرين لسباقات الهجن مما وضعها على طريق التوهج المستمر وفتح الطريق لحصد الإنجازات والتوشح بالألقاب وانعكس ذلك على هجن الرئاسة والتي تخوض التحديات بحثاً عن القمة في كافة المحافل الداخلية والخارجية.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وأوضح معاليه أن دعم ومتابعة سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة لسباقات الهجن وتوفير كافة المتطلبات ظل يصب في اتجاه تطويرها والوصول بها إلى أعلى المراتب، وما إنجاز هجن الرئاسة في مهرجان أمير قطر إلا محصلة ونتاج طبيعي للدعم الكبير الذي يقدمه سموه لرياضة سباقات الهجن.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وأوضح معاليه «سباقات الهجن في الإمارات محظوظة بالاهتمام الكبير والدعم الذي تجده من القيادة الرشيدة التي تحرص على غرس هذا الإرث التاريخي في نفوس الشباب لتواصل رياضة الآباء والأجداد مسيرتها مع الأجيال والتي تحمل العديد من المعاني العميقة في نفوس الجميع».<\/div><div style=\"text-align: right;\">وأشار معاليه إلى أن هجن الرئاسة تخوض كل التحديات للتتويج بالناموس في السباقات التي تقام محلياً أو في المنافسات الخارجية لذا تأتي مشاركاتها مليئة بالتحدي بحثاً عن التتويج، وهنا نجد أن تتويجها بسيف أمير قطر في الشحانية وبستة رموز أخرى هو امتداد لإنجازات متتالية ظلت تحققها على الدوام، ونأمل أن تتواصل مسيرة نجاحاتها في كل السباقات المقبلة.<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic89046723.jpg"
            },
            {
               "news_date": "الأحد ، ٠٨ ديسمبر  ٢٠١٣",
               "news_title": "سمو الشيخ منصور بن زايد آل نهيان يحضر مأدبة غداء أقامها معالي الشيخ سلطان بن حمدان آل نهيان",
               "news_summary": "حضر سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مأدبة غداء أقامها لسموه معالي الشيخ سلطان بن حمدان بن محمد آل نهيان، مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن.",
               "news": "<div style=\"text-align: right;\">حضر سمو الشيخ منصور بن زايد آل نهيان نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مأدبة غداء أقامها لسموه معالي الشيخ سلطان بن حمدان بن محمد آل نهيان، مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن. حضر المأدبة الشيخ سعيد بن حمدان بن محمد آل نهيان، والشيخ خليفة بن سلطان بن حمدان آل نهيان، وعدد من الملاك من أبناء الدولة ودول مجلس التعاون الخليجي، وقدم الحضور التهنئة لسموه بمناسبة فوز إسطبلات الوثبة بكأس اليوم الوطني للقدرة، بعد إحراز الفارس علي يوسف الحمادي المركز الأول على ظهر الفرس (آرمور).<\/div>",
               "image": "http://www.prcamels.ae/images/news/npic92061215.jpg"
            }
         ]
      },
      {
         "section_title": "السباقات",
         "news": [
            {
               "news_date": "الأربعاء ، ١٩ مارس  ٢٠١٤",
               "news_title": "الوارية تنتزع سيف صاحب السمو رئيس الدولة",
               "news_summary": "أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله،",
               "news": "<p align=\"right\">أهدى معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، إنجاز «هجن الرئاسة» وفوزها بسيفي الحول والزمول، وخنجر الزمول المحليات، إلى صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس الدولة حفظه الله، مؤكداً أن فوز هجن الرئاسة بـ «ناموس القائد»، يمثل أعلى وأروع تكريم لمسيرة إنجازاتها المتلاحقة، التي توجتها أمس بتتويج مستحق واستثنائي، مشيراً إلى أن ما شهده المهرجان الختامي بالوثبة، يمثل شاهداً حياً على دعم وعطاء القيادة للرياضات التراثية، وفي مقدمتها سباقات الهجن التي تعيش عصرها الذهبي. <\/p><p align=\"right\">كما أهدى معاليه إنجاز «هجن الرئاسة» إلى الفريق أول سمو الشيخ محمد بن زايد آل نهيان ولي عهد أبوظبي نائب القائد الأعلى للقوات المسلحة، مشيداً بدعم سموه المتواصل للرياضة والرياضيين، وللرياضات التراثية بصفة خاصة، وفي مقدمتها سباقات الهجن، التي يحفظ منتسبوها لسموه أياديه البيضاء على هذه الرياضة، والتي تمثلت في عدة سباقات تطوف إمارات الدولة باسم سموه، إضافة إلى الدعم الدائم والسخي للسباقات في كل الميادين، ما جعل الإمارات وجهة للملاك والمضمرين من دول الخليج كافة. <\/p><p align=\"right\">وثمن معالي الشيخ سلطان بن حمدان آل نهيان مستشار صاحب السمو رئيس الدولة، رئيس اتحاد سباقات الهجن، دعم وعطاء سمو الشيخ منصور بن زايد آل نهيان، نائب رئيس مجلس الوزراء وزير شؤون الرئاسة، مؤكداً أن عطاء سموه بلا حدود، وأن ما شهدته سباقات الهجن ومختلف الرياضات التراثية من تطور، كان نتاجاً لأفكار سموه الوثابة التي لا تحدها حدود، وتنطلق من حب عميق للوطن، ومن ربط رائع بين الماضي بموروثه العريق والحاضر بمعطيات التطور، تطلعاً إلى مستقبل حافل بإذن الله لدولتنا الناهضة. <br><\/p>",
               "image": "http://www.prcamels.ae/images/news/npic18711720.jpg"
            },
            {
               "news_date": "الأحد ، ١٦ مارس  ٢٠١٤",
               "news_title": "الخفيفة لـ «هجن الرئاسة» تتصدر منافسات الشيوخ للثنايا",
               "news_summary": "شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي",
               "news": "<div style=\"text-align: right;\">شهد معالي الشيخ سلطان بن حمدان بن محمد آل نهيان مستشار صاحب السمو رئيس الدولة حفظه الله، رئيس اتحاد سباقات الهجن، منافسات اليوم السادس للمهرجان الختامي السنوي لسباقات الهجن بمنطقة الوثبة بالعاصمة أبوظبي، والتي خصصت لسن (الثنايا) الطفح ولمسافة 6 كلم، كما شهد المنافسات، جمهور غفير من محبي رياضة سباقات الهجن من أبناء الدولة وإخوانهم من دول مجلس التعاون الخليجي.<\/div><div style=\"text-align: right;\"><br><\/div><div style=\"text-align: right;\">وتصدرت (الخفيفة) لهجن الرئاسة مع المضمر مبارك أحمد مطر الخييلي الشوط الأول والمخصص للبكار المحليات وأحرزت المركز الأول والناموس بعد أن قطعت مسافة الشوط في توقيت (9:15:2 دقيقة)، وجاءت في المركز الثاني (احتلال) لهجن الرئاسة والمضمر عيسى أحمد مطر الخييلي بتوقيت وقدره(9:17:2)، وحلت ثالثة (الوجبة) لهجن الشحانية والمضمر سالم فاران المري بتوقيت (9:20:00).<\/div><div style=\"text-align: right;\"><br><\/div>",
               "image": "http://www.prcamels.ae/images/news/npic27449365.jpg"
            }
         ]
      }
   ]
}

I want to load data of the first array section from this JSON code and display all arrays within that array plus there is a link to image file which I want to display as well. what will be the best approach to display that data along with the picture, please note that the data will be displayed within a scroll view or advice the best possible way to show the data other than scroll view because there are other elements on that page and only half of the acclivity size is available for this data.

The following data will be displayed from the JSON array:

News_Date
News_Title
News_Summary

I want to cover 20% of the Area with Picture and 80% with the Text in A Row and for each item the rows will be repeated.
 

Shahid Saeed

Active Member
Licensed User
Longtime User
Dear @Erel,

Can you please help me with this I have been trying to do that but I am not successful because of my limited knowledge.

This is what I get from JSONTree:

B4X:
Dim root As Map = parser.NextObject
Dim news_sections As List = root.Get("news_sections")
For Each colnews_sections As Map In news_sections
    Dim section_title As String = colnews_sections.Get("section_title")
    Dim news As List = colnews_sections.Get("news")
    For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news As String = colnews.Get("news")
        Dim image As String = colnews.Get("image")
        Dim news_title As String = colnews.Get("news_title")
    Next
Next

Now I don't know How to Display Data only arrays of First Array.

Can you also guide me how to download text with HttpUtil2 and parse it?
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
@Erel

I am sorry for being so dumb, I have gone through your tutorial which leads to downloading data from a webpage and displays images; I am totally confused with the web link of JSON I have, I am still not understanding how to download data and parse it.
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
@Erel

Some how after reading the tutorials i am able to load the Json file but still I am not able to achieve what i need to do with Jason data and display in a scroll view. This is what I trying: Please point out the mistakes, I am sorry for being so dumb.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim NewsUrl As String
    NewsUrl = "http://www.prcamels.ae/json/news.json.php"
    Dim NewsJson As String
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim ScrollView1 As ScrollView
    Dim lblDate As Label
    Dim news_date As String
    Dim lblTitle As Label
    Dim news_title As String
    Dim lblSummary As Label
    Dim news_summary As String
    Dim image As ImageView
    Dim StrUtil As StringUtils
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("News")   
   
    Dim GetPage As HttpJob
    GetPage.Initialize("GetPageJob", Me)
    GetPage.Download(NewsUrl)
    ProgressDialogShow2("Fetching data...", False)
   
    Dim JSON As JSONParser
    Dim Map1 As Map
    JSON.Initialize(NewsJson)
    Dim root As Map = JSON.NextObject
    Dim news_sections As List = root.Get("news_sections")
    For Each colnews_sections As Map In news_sections
    Dim section_title As String = colnews_sections.Get("section_title")
    Dim news As List = colnews_sections.Get("news")
    For Each colnews As Map In news
        news_summary = colnews.Get("news_summary")
        news_date = colnews.Get("news_date")
        'Dim news2 As String = colnews.Get("news")
        image = colnews.Get("image")
        news_title = colnews.Get("news_title")
               
        lblDate.Initialize("")
        ScrollView1.Panel.AddView(lblDate, 0, 0, 80%x, 10%y)
        'lblTitle.Color = Colors.RGB(250, 250, 210)
        lblDate.TextColor = Colors.Black
               
        lblTitle.Initialize("")
        ScrollView1.Panel.AddView(lblTitle, 0, 0, 80%x, 10%y)
        'lblTitle.Color = Colors.RGB(250, 250, 210)
        lblTitle.TextColor = Colors.Black
       
        lblSummary.Initialize("")
        ScrollView1.Panel.AddView(lblSummary, 0, 0, 80%x, 10%y)
        'lblTitle.Color = Colors.RGB(250, 250, 210)
        lblTitle.TextColor = Colors.Black
       
        'image.Initialize("")
        'ScrollView1.Panel.Addview(image, 0, 0, 20%X, 30%y)
       
       
        'Dim ht As Float
       
        lblDate.Text = news_date
        lblTitle.Text = news_title
        lblSummary.Text = news_summary
        'image.SetBackgroundImage = image
       
        'ht = StrUtil.MeasureMultilineTextHeight(lblTitle, news_title)
        'ScrollView1.Panel.Height = ht
        'lblTitle.Height = ht
        ScrollView1.ScrollPosition = 0
        DoEvents
       
    Next
Next
   
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success = True Then
        NewsJson = Job.GetString
        ProgressDialogHide
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
End Sub
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
I am still stuck with the JSON parser, I really don't know how to get the Array of first Section:

The JSON code I am working with

B4X:
{
   "news_sections": [
      {
         "section_title": "title",
         "news": [
            {
               "news_date": "news date",
               "news_title": "news title",
               "news_summary": "news summary",
               "news": "News Data",
               "image": "image url"
            },
            {
               "news_date": "news date",
               "news_title": "news title",
               "news_summary": "news summary",
               "news": "News Data",
               "image": "image url"
            }
         ]
      },
      {
         "section_title": "title",
         "news": [
            {
               "news_date": "news date",
               "news_title": "news title",
               "news_summary": "news summary",
               "news": "News Data",
               "image": "image url"
            },
            {
               "news_date": "news date",
               "news_title": "news title",
               "news_summary": "news summary",
               "news": "News Data",
               "image": "image url"
            }
         ]
      },
      {
         "section_title": "title",
         "news": [
            {
               "news_date": "news date",
               "news_title": "news title",
               "news_summary": "news summary",
               "news": "News Data",
               "image": "image url"
            },
            {
               "news_date": "news date",
               "news_title": "news title",
               "news_summary": "news summary",
               "news": "News Data",
               "image": "image url"
            }
         ]
      }
   ]
}

As you can see there are three arrays correspoding through section_title and each section further have array. What i need is to get inner arrays of the first section and display the data in a scroll view.

This is what I'm getting from JSONTree:

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim news_sections As List = root.Get("news_sections")
For Each colnews_sections As Map In news_sections
    Dim section_title As String = colnews_sections.Get("section_title")
    Dim news As List = colnews_sections.Get("news")
    For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news As String = colnews.Get("news")
        Dim image As String = colnews.Get("image")
        Dim news_title As String = colnews.Get("news_title")
    Next
Next

I am not able to load all arrays, and display them in a scroll view, In my previous attempt; It is showing only last array data.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
on first iteration of this code

B4X:
    For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news As String = colnews.Get("news")
        Dim image As String = colnews.Get("image")
        Dim news_title As String = colnews.Get("news_title")
    Next

you get the first news. On second iteration you get the second. and so on...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
For Each colnews As Map In news
  Dim news_summary As String = colnews.Get("news_summary")
  Dim news_date As String = colnews.Get("news_date")
  Dim news As String = colnews.Get("news")
  Dim image As String = colnews.Get("image")
  Dim news_title As String = colnews.Get("news_title")
  ' create new entry in scrollview here. news_summary, news_date, news, image and news_title are set... do the work to create entry
Next

you probably want to try a LOG first...

B4X:
For Each colnews As Map In news
  Dim news_summary As String = colnews.Get("news_summary")
  Dim news_date As String = colnews.Get("news_date")
  Dim news As String = colnews.Get("news")
  Dim image As String = colnews.Get("image")
  Dim news_title As String = colnews.Get("news_title")
  log(news_title)
Next
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
I am Getting The following error:

B4X:
LogCat connected to: emulator-5554
** Activity (main) Create, isFirst = true **


java.lang.RuntimeException: Object should first be initialized (Label).


    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:46)
    at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
    at b4a.example.main._activity_create(main.java:307)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at b4a.example.main.afterFirstLayout(main.java:98)
    at b4a.example.main.access$100(main.java:16)
    at b4a.example.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)


    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (Label).
** Service (httputils2service) Create **


** Service (httputils2service) Start **


** Service (httputils2service) Start **


** Activity (main) Resume **


Error: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=


java.lang.NumberFormatException: Invalid double: "news"


    at org.apache.harmony.luni.util.FloatingPointParser.invalidReal(FloatingPointParser.java:78)
    at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:135)
    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:290)
    at java.lang.Double.parseDouble(Double.java:317)
    at b4a.example.main._jobdone(main.java:408)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:957)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)


    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NumberFormatException: Invalid double: "news"
 

Attachments

  • NewsTest.zip
    7.7 KB · Views: 257
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
which object?

Can you please check the code and let me know?

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim NewsUrl As String
    NewsUrl = "http://www.prcamels.ae/json/news.json.php"
    Dim NewsJson As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim ScrollView1 As ScrollView
    Dim lblDate As Label
    Dim news_date As String
    Dim lblTitle As Label
    Dim news_title As String
    Dim lblSummary As Label
    Dim news_summary As String
    Dim ImageView1 As ImageView
    Dim newsimage As ImageView
    Dim news_image As String
    Dim news_news As String
    Dim StrUtil As StringUtils

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("TestNews")
    Dim GetPage, GetImage As HttpJob
    GetPage.Initialize("GetPageJob", Me)
    GetPage.Download(NewsUrl)
    ProgressDialogShow2("يرجى الانتظار جاري التحميل ...", False)
   
    GetImage.Initialize("GetImageJob", Me)
    GetImage.Download(news_image)
   
    'Dim ht As Float
       
        lblDate.Text = news_date
        lblTitle.Text = news_title
        lblSummary.Text = news_summary
        newsimage.SetBackgroundImage(GetImage.GetBitmap)
        ImageView1.SetBackgroundImage(GetImage.GetBitmap)
       
        'ht = StrUtil.MeasureMultilineTextHeight(lblTitle, news_title)
        'ScrollView1.Panel.Height = ht
        'lblTitle.Height = ht
        ScrollView1.ScrollPosition = 0
        DoEvents
       
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success = True Then
        NewsJson = Job.GetString
        Dim JSON As JSONParser
       'Dim Map1 As Map
       JSON.Initialize(NewsJson)
       Dim root As Map = JSON.NextObject
        Dim news_sections As List = root.Get("news_sections")
        For Each colnews_sections As Map In news_sections
        Dim section_title As String = colnews_sections.Get("section_title")       
        Dim news As List = news_sections.Get("news")
        For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news_news As String = colnews.Get("news")
        Dim news_image As String = colnews.Get("image")
        Dim news_title As String= colnews.Get("news_title")
        Log(news_title)
       
        lblDate.Initialize("")
        ScrollView1.Panel.AddView(lblDate, 0, 0, 80%x, 10%y)       
        'lblTitle.Color = Colors.RGB(250, 250, 210)
        lblDate.TextColor = Colors.Black
               
        lblTitle.Initialize("")
        ScrollView1.Panel.AddView(lblTitle, 0, 10, 80%x, 10%y)
        'lblTitle.Color = Colors.RGB(250, 250, 210)
        lblTitle.TextColor = Colors.Black
       
        lblSummary.Initialize("")
        ScrollView1.Panel.AddView(lblSummary, 0, 20, 80%x, 10%y)
        'lblTitle.Color = Colors.RGB(250, 250, 210)
        lblTitle.TextColor = Colors.Black
       
               
        newsimage.Initialize("")
        ScrollView1.Panel.Addview(newsimage, 0, 0, 20%X, 30%y)
           
        Next
        Next
        ProgressDialogHide
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Actually there are multiple errors there...

One at a time though!

The first error is because you haven't initialised a Label. I would generate with trace and see the line that is raising this error. Then fix this. Before you can assign a value you need to initialise the variable, in this case a Label.

Then let's see what the next error is..
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Error: java.lang.IllegalStateException: Target host must not be null

Target host needs to be set. I suspect that you haven't posted the code which performs this though..
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
OK I have reduced the workflow to go step by step:

This is the reduced code to download the file from server and parse it with JSON parser

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim NewsUrl As String
    NewsUrl = "http://www.prcamels.ae/json/news.json.php"
    Dim NewsJson As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim news_summary As String
    Dim news_date As String
    Dim news_news As String
    Dim news_image As String
    Dim news_title As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("TestNews")
    Dim GetPage, GetImage As HttpJob
    GetPage.Initialize("GetPageJob", Me)
    GetPage.Download(NewsUrl)
    ProgressDialogShow2("يرجى الانتظار جاري التحميل ...", False)
   
    GetImage.Initialize("GetImageJob", Me)
    GetImage.Download(news_image)
   
   
       
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success = True Then
        NewsJson = Job.GetString
        Dim JSON As JSONParser
       'Dim Map1 As Map
       JSON.Initialize(NewsJson)
       Dim root As Map = JSON.NextObject
        Dim news_sections As List = root.Get("news_sections")
        For Each colnews_sections As Map In news_sections
        Dim section_title As String = colnews_sections.Get("section_title")       
        Dim news As List = news_sections.Get("news")
        For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news_news As String = colnews.Get("news")
        Dim news_image As String = colnews.Get("image")
        Dim news_title As String= colnews.Get("news_title")
        Log(news_title)
       
               
        Next
        Next
        ProgressDialogHide
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

This is the error message:

B4X:
** Activity (main) Pause, UserClosed = true **


** Activity (main) Create, isFirst = false **


** Activity (main) Resume **


Error: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=


java.lang.NumberFormatException: Invalid double: "news"


    at org.apache.harmony.luni.util.FloatingPointParser.invalidReal(FloatingPointParser.java:78)
    at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:135)
    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:290)
    at java.lang.Double.parseDouble(Double.java:317)
    at b4a.example.main._jobdone(main.java:373)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:957)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)


    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NumberFormatException: Invalid double: "news"
 
Upvote 0

eps

Expert
Licensed User
Longtime User
You are sending NewJson as the string, but this doesn't get assigned, are you not supposed to send it NewsUrl?

B4X:
 JSON.Initialize(NewsJson)
 
Upvote 0

Shahid Saeed

Active Member
Licensed User
Longtime User
I have made some changes into the code I have moved the ImageDownload Job under the Jason praser and now the next error is there:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim NewsUrl As String
    NewsUrl = "http://www.prcamels.ae/json/news.json.php"
    Dim NewsJson As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim news_summary As String
    Dim news_date As String
    Dim news_news As String
    Dim news_image As String
    Dim news_title As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("TestNews")
    Dim GetPage As HttpJob
    GetPage.Initialize("GetPageJob", Me)
    GetPage.Download(NewsUrl)
    ProgressDialogShow2("يرجى الانتظار جاري التحميل ...", False)
   
   
   
   
       
End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success = True Then
        NewsJson = Job.GetString
        Dim JSON As JSONParser
       'Dim Map1 As Map
       JSON.Initialize(NewsJson)
       Dim root As Map = JSON.NextObject
        Dim news_sections As List = root.Get("news_sections")
        For Each colnews_sections As Map In news_sections
        Dim section_title As String = colnews_sections.Get("section_title")       
        Dim news As List = news_sections.Get("news")
        For Each colnews As Map In news
        Dim news_summary As String = colnews.Get("news_summary")
        Dim news_date As String = colnews.Get("news_date")
        Dim news_news As String = colnews.Get("news")
        Dim news_image As String = colnews.Get("image")
        Dim news_title As String= colnews.Get("news_title")
        Log(news_title)
       
        Dim GetImage As HttpJob
        GetImage.Initialize("GetImageJob", Me)
        GetImage.Download(news_image)
       
               
        Next
        Next
        ProgressDialogHide
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

The Error:

B4X:
** Activity (main) Create, isFirst = false **


** Activity (main) Resume **


java.lang.NumberFormatException: Invalid double: "news"


    at org.apache.harmony.luni.util.FloatingPointParser.invalidReal(FloatingPointParser.java:78)
    at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:135)
    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:290)
    at java.lang.Double.parseDouble(Double.java:317)
    at b4a.example.main._jobdone(main.java:368)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:957)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:132)
    at android.app.ActivityThread.main(ActivityThread.java:4123)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:491)


    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NumberFormatException: Invalid double: "news"
 
Upvote 0
Top