httpclient method get android not working -
When I try to call the PHP function (method received) from an Android app, I have some problems. The app contains the code:
try {string link = "http: // localhost? Name =" + name; Println (link); DefaultHttpClient httpclient = New DefaultHttpClient (); HttpGet httpget = new HttpGet (link); HttpResponse response = httpclient.execute (httpget); BufferedReader = New in BufferedReader (New InputStreamReader (response.getEntity). GetContent ())); Stringbuffer sb = new stringbuffer (""); String line = ""; String nl = System.getProperty ("line.separator"); While ((line = in.readline ()) = null) {sb.append (line + nl); } in.close (); String result = sb.toString (); Logs. V ("My Answer:" result); } Hold (exception e) {Log.i ("error", ("exception:" + e.getMessage ()); } and the server side has php code:
& lt; Php $ con = mysqli_connect ("localhost", "root", "", "emergency"); If (mysqli_connect_errno ($ con)) {echo "failed to join MySQL:". Mysqli_connect_error (); } $ Name = $ _GET ['name']; $ Sql = "Data (name, age, cell, gender, status, ocupation, origin, home, contact name, phone1, phone2, latitude, longitude) value ('". $ Name ", 23,' 22256602 ' 'True', 'soltero', 'empleado', 'mexicano', 'puebla', 'marua', '552,786', '', '19.08, 306', '- 9 8.20,621') "; $ Sql per second "& lt; / br> & lt; / br & gt; & lt; / br & gt;"; If mysqli_query ($ con, $ sql) (die ('error:'. Mysqli_error ($ con)); "added 1 record" echo; mysqli_close ($ thief);? & Gt; < / pre> The problem is that I have tried in different ways but still gives me a blank exception .. What can I do?: S tired you too much!
You should not establish a network connection from the UI thread, so you get an exception:
Exception: android.os.NetworkOnMainThreadException For example, one of the main reasons is that asynchronous method should be done to avoid a frozen UI.
Comments
Post a Comment