android - how to convert my java object to json and save it in sd card -


i don't know how after convert java json jackson

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      objectmapper mapper = new objectmapper();      book b = new book();     arraylist<book> listbook = new arraylist<book>();     listbook.add(b);      b = new book();     b.setname("exam");     b.setfilename("res/raw/exam.txt");     b.setcate(0); //4 categories here     b.setsoundfilename("res/raw/exams.mp3");     b.settranfilename("res/raw/examt.txt");     listbook.add(b);      b = new book();     b.setname("test");     b.setcate(0);     b.setfilename("res/raw/test.txt");     b.setsoundfilename("res/raw/tests.mp3")     b.settranfilename("res/raw/textt.txt");      string jsonstring = "";     try {         jsonstring = mapper.writevalueasstring(listbook);     } catch (jsongenerationexception e) {          e.printstacktrace();     } catch (jsonmappingexception e) {          e.printstacktrace();     } catch (ioexception e) {          e.printstacktrace();     }      log.d("ttt", "show ans : "+jsonstring); 

how do next write sd card?

as user zain has told, gson more convenient use, however, assuming getting json string line:

jsonstring = mapper.writevalueasstring(listbook); 

then use method write string file in sdcard.

public static void writestringtofile(string p_string, string p_filename)     {         fileoutputstream m_stream = null;         try         {             m_stream = new fileoutputstream(p_filename);             m_stream.write(p_string.getbytes());             m_stream.flush();         }         catch (throwable m_th)         {             log.e(tag + " error in writestringtofile(string p_string, string p_filename) of fileio", m_th);         }                 {             if (m_stream != null)             {                 try                 {                     m_stream.close();                 }                 catch (throwable m_e)                 {                     log.e(tag + " error in writestringtofile(string p_string, string p_filename) of fileio", m_e);                 }                 m_stream = null;             }         }     } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -