java - Error parsing Json from a php script to display it in android app -
my goal data of mssql database, part done , working, , grab json , display in android application.
the data gets app because displayed on logcat , in 1 toast message, not complete process.
the json long, , when try see error on logcat, cant because doesnt show info, here meant:
there lot more data guess doesnt fit on console..
the json im working with: http://ploran.gear.host/scriptobras6.php
php:
<?php error_reporting(1); ini_set('mssql.charset', 'utf-8'); $servername = "--"; /* uid , pwd application-specific files. */ $connectioninfo = array( "database"=>"programaplo", "uid"=>"--", "pwd"=>"--","characterset"=>"utf-8"); $conn = sqlsrv_connect($servername, $connectioninfo); // if( $conn ) { // echo "connection established.<br />"; // }else{ // echo "connection not established.<br />"; // die( print_r( sqlsrv_errors(), true)); // } $tsql = "select * obras"; $stmt = sqlsrv_query($conn, $tsql); // if( $stmt === false ) { // echo "error in executing query.</br>"; // die( print_r( sqlsrv_errors(), true)); // } // echo "query: sucesso \n"; $json = array(); while ($row = sqlsrv_fetch_array($stmt, sqlsrv_fetch_assoc)) { $json[] = $row; } header('content-type: application/json; charset=utf-8'); echo json_encode($json); sqlsrv_free_stmt($stmt); sqlsrv_close($conn); //close connnectiokn first exit(); ?>
mainactivity.java
public class mainactivity extends activity { private string tag = mainactivity.class.getsimplename(); private progressdialog pdialog; private listview list; private static string url = "http://ploran.gear.host/scriptobras6.php"; arraylist<hashmap<string, string>> obraslist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); obraslist = new arraylist<>(); list = (listview)findviewbyid(r.id.list1); new getobras().execute(); } private class getobras extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("please wait..."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { httphandler sh = new httphandler(); // making request url , getting response string jsonstr = sh.makeservicecall(url); log.e(tag, "response url: " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node jsonarray obras = jsonobj.getjsonarray("obras"); // looping through (int = 0; < obras.length(); i++) { jsonobject c = obras.getjsonobject(i); string id = c.getstring("id"); string nomeobra = c.getstring("nomeobra"); string idcliente = c.getstring("idcliente"); string datalevantamento = c.getstring("dataplevantamento"); string datarealizacao = c.getstring("datarlevantamento"); string estado = c.getstring("estado"); string datamateriais = c.getstring("datarmateriais"); string datainicioobra = c.getstring("datainicioobra"); string dataconclusao = c.getstring("dataconclusao"); string datavestoria = c.getstring("datavestoria"); string obs = c.getstring("obs"); string prompor = c.getstring("prompor"); string levantpor = c.getstring("levantpor"); string executpor = c.getstring("executpor"); // tmp hash map single contact hashmap<string, string> obra = new hashmap<>(); // adding each child node hashmap key => value obra.put("id", id); obra.put("nomeobra", nomeobra); obra.put("idcliente", idcliente); obra.put("datalevantamento", datalevantamento); obra.put("datarealizacao", datarealizacao); obra.put("estado", estado); obra.put("datamateriais", datamateriais); obra.put("dataincioobra", datainicioobra); obra.put("dataconclusao", dataconclusao); obra.put("datavestoria", datavestoria); obra.put("obs", obs); obra.put("prompor", prompor); obra.put("levantpor", levantpor); obra.put("executpor", executpor); // adding contact contact list obraslist.add(obra); } } catch (final jsonexception e) { log.e(tag, "json parsing error: " + e.getmessage()); logger.debugentire("teste", e); runonuithread(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "json parsing error: " + e.getmessage(), toast.length_long) .show(); } }); } } else { log.e(tag, "couldn't json server."); runonuithread(new runnable() { @override public void run() { toast.maketext(getapplicationcontext(), "couldn't json server. check logcat possible errors!", toast.length_long) .show(); } }); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( mainactivity.this, obraslist, r.layout.list_item, new string[]{"nomeobra", "idcliente", "estado"}, new int[]{r.id.name, r.id.email, r.id.mobile}); list.setadapter(adapter); } } }
httphandler.java
public class httphandler { private static final string tag = httphandler.class.getsimplename(); public httphandler() { } public string makeservicecall(string requrl) { string response = null; try { url url = new url(requrl); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); // read response inputstream in = new bufferedinputstream(conn.getinputstream()); response = convertstreamtostring(in); } catch (malformedurlexception e) { log.e(tag, "malformedurlexception: " + e.getmessage()); } catch (protocolexception e) { log.e(tag, "protocolexception: " + e.getmessage()); } catch (ioexception e) { log.e(tag, "ioexception: " + e.getmessage()); } catch (exception e) { log.e(tag, "exception: " + e.getmessage()); } return response; } private string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line; try { while ((line = reader.readline()) != null) { sb.append(line).append('\n'); } } catch (ioexception e) { e.printstacktrace(); } { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } }
i bit lost here, please take @ php "while" solutions saw on web bit different way found json, can issue, or not..
Comments
Post a Comment