android - php Json getting a blank textview -
i using mysql database in host, want recive data host when data textview, in blank.
public class mainactivity extends activity { textview tv1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv1=(textview)findviewbyid(r.id.name); loader loader = new loader(getapplicationcontext()); tv1.settext(loader.loadinbackground()); } public static class loader extends asynctaskloader<string>{ public loader (context contexto) { super(contexto); } @override public string loadinbackground() { string resultado = ""; string entrada = ""; defaulthttpclient cliente = new defaulthttpclient(); httpget httpget = new httpget("http://comupunt.esy.es/cities.php"); try { httpresponse execute = cliente.execute(httpget); inputstream contenido = execute.getentity().getcontent(); bufferedreader buffer = new bufferedreader(new inputstreamreader(contenido)); string s=""; while((s=buffer.readline())!=null) resultado+=s; { } } catch (exception e) { // todo: handle exception } try { jsonobject object = new jsonobject(resultado); jsonarray jarray = object.getjsonarray("cities"); (int i=0;i<jarray.length();i++) { jsonobject jobject = jarray.getjsonobject(i); string name=jobject.getstring("name"); entrada += name; } } catch (exception e) { // todo: handle exception log.e("webservice", e.getmessage()); } return entrada; } } }
the php json this
{"cities":[{"name":"aitor"}]}
and logcat:
12-02 04:24:01.492: e/webservice(2190): end of input @ character 0 of
thanks
you in mainactivity line
httpresponse execute = cliente.execute(httpget);
is throwing networkonmainthreadexception can check using (in first try catch well)
catch (exception e) { // todo: handle exception log.e("errormsg", e.tostring(); }
so instead of using asynctaskloader better use asynctask.
public class mainactivity extends activity { textview tv1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv1 = (textview) findviewbyid(r.id.text); exe exe = new exe(); try { uri uri = new uri("http://comupunt.esy.es/cities.php"); } catch (urisyntaxexception e) { // todo auto-generated catch block e.printstacktrace(); } exe.execute(); } class exe extends asynctask<url, string, string> { string entrada = ""; @override protected string doinbackground(url... url) { string resultado = ""; try { defaulthttpclient cliente = new defaulthttpclient(); defaulthttpclient httpclient = new defaulthttpclient(); uri uri = new uri("http://comupunt.esy.es/cities.php"); httpget http = new httpget(uri); httpresponse execute = cliente.execute(http); inputstream contenido = execute.getentity().getcontent(); bufferedreader buffer = new bufferedreader( new inputstreamreader(contenido)); string s = ""; while ((s = buffer.readline()) != null) { resultado += s; } jsonobject object = new jsonobject(resultado); log.e("webservice1", object.tostring()); jsonarray jarray = object.getjsonarray("cities"); (int = 0; < jarray.length(); i++) { jsonobject jobject = jarray.getjsonobject(i); string name = jobject.getstring("name"); entrada = name; } } catch (exception e) { // todo: handle exception log.e("webservice", e.getmessage()); } return entrada; } @override protected void onpostexecute(string res) { tv1.settext(entrada); } }
}
Comments
Post a Comment