read from json (string) android with Gson -


its json file

{"visitorslist":[{"visitorid":"09005451","visitorname":" xxxx","visitorphon":"","visitoraddr":"xxxx","geocode":"","autokey":1},{"visitorid":"09005468","visitorname":"xxxxxx","visitorphon":"09005468","visitoraddr":"xxxx","geocode":"","autokey":2}]} 

and wanna read , show information file listview

my visitorslistactivity is:

public class visitorslistactivity extends appcompatactivity {  public listview lstvisitors; static visitorslist visitorslist;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.visitor_list);      try {          //********************************** comelete section *********************************************          new jsonhelper.getjsondata(new jsonhelper.getjsondata.asyncresponse() {              @override             public void processfinish(string output) {                 try {                     if (output == null) output = "";                     if (output.equals("")) {                         toast.maketext(getapplicationcontext(),                                 "no visitor founded",                                 toast.length_long)                                 .show();                         return;                     }                     //960105--------------------                     else if (output.equals("401")) {                         toast.maketext(getapplicationcontext(),                                 "error 401",                                 toast.length_long)                                 .show();                         return;                     }                     //--------------------------------------                      log.i("log", "output" + output);                     gson gson = new gson();                     output = output.substring(1, output.length() - 1);                      /*toast.maketext(getapplicationcontext(),                             output,                             toast.length_long)                             .show();*/                      visitorslist = new visitorslist();                     visitorslist = gson.fromjson(output, visitorslist.class);                  } catch (exception e) {                     e.printstacktrace();                      toast.maketext(getapplicationcontext(),                             "exception",                             toast.length_long)                             .show();                 }             }         }).execute("http://192.168.1.162:8014/api/visitors");           //************************************ visitor list null :| *******************************************           /*if( visitorslist == null) {             toast.maketext(getapplicationcontext(),                     "data nul",                     toast.length_long)                     .show();         }*/          adaptervisitor customadapter = new adaptervisitor(this, r.layout.visitor_list, visitorslist.visitorslist);         customadapter.notifydatasetchanged();         lstvisitors.setadapter(customadapter);          lstvisitors.requestfocus();          final viewgroup layoutclear = (viewgroup) findviewbyid(r.id.layoutclear);         layoutclear.setvisibility(view.gone);      } catch (exception e) {         e.getmessage();     }  } } 

and visitorslist:

public class visitorslist {  public visitorslist() {}  public arraylist<visitors> visitorslist;} 

visitors:

public class visitors { public string visitorid; public string visitorname; public string visitorphon; public string visitoraddr; public string geocode; public int autokey;} 

and visitor list created gson null. dont know maybe problem in reading json or maybe visitors class ...

jsonhelper :

public class jsonhelper {  public static class getjsondata extends asynctask<string, void, string> {      public interface asyncresponse {          void processfinish(string output);     }      public asyncresponse delegate = null;       public getjsondata(asyncresponse delegate) {         this.delegate = delegate;     }       @override     protected string doinbackground(string... strurl) {          string str = strurl[0];         urlconnection urlconn = null;         bufferedreader bufferedreader = null;         try {             url url = new url(str);             urlconn = url.openconnection();             urlconn.setreadtimeout(300000);             urlconn.setconnecttimeout(5000);              bufferedreader = new bufferedreader(new inputstreamreader(urlconn.getinputstream(), "utf-8"), 8);              stringbuffer stringbuffer = new stringbuffer();             string line;             while ((line = bufferedreader.readline()) != null) {                 stringbuffer.append(line);             }             string tmpjson = stringbuffer.tostring().replace("\\", "");              return tmpjson;         } catch (exception ex) {             ex.getmessage();              return null;         } {             if (bufferedreader != null) {                 try {                     bufferedreader.close();                 } catch (ioexception e) {                     e.printstacktrace();                  }             }         }     }       @override     protected void onpostexecute(string response) {         delegate.processfinish(response);     } }   public static class setjsondata extends asynctask<string, void, string> {      string responseserver;       public interface asyncresponse {          void processfinish(string output);     }      public asyncresponse delegate = null;       public setjsondata(asyncresponse delegate) {         this.delegate = delegate;     }       @override     protected string doinbackground(string... param) {         url url;         string response = null;         try {              url = new url(param[0]);             httpurlconnection conn = null;             conn = (httpurlconnection) url.openconnection();             conn.setreadtimeout(20000);             conn.setconnecttimeout(10000);//950718             conn.setrequestmethod("get");             conn.setdoinput(true);             conn.setdooutput(true);             outputstream os = conn.getoutputstream();             bufferedwriter writer = new bufferedwriter(new outputstreamwriter(os, "utf-8"));             writer.write(param[1]);             writer.flush();             writer.close();             os.close();             int responsecode = conn.getresponsecode();             if (responsecode == httpsurlconnection.http_ok) {                 string line;                 bufferedreader br = new bufferedreader(new inputstreamreader(conn.getinputstream()));                 while ((line = br.readline()) != null) {                     response += line;                 }             } else {                 response = (string.valueof(responsecode));                 string line;                 bufferedreader br = new bufferedreader(new inputstreamreader(conn.geterrorstream()));//950427                 while ((line = br.readline()) != null) {                 }              }             return response;         } catch (ioexception e1) {             e1.getmessage();              return null;         } catch (exception e) {              e.getmessage();             return null;         }     }      @override     protected void onpostexecute(string result) {         // todo auto-generated method stub         try {             super.onpostexecute(result);             delegate.processfinish(result);         } catch (exception e) {             e.getmessage();         }     }  }} 

your visitors class missing annotations. use convert properly: http://www.jsonschema2pojo.org/


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -