php - "Couldn't connect to database" error on Android -
I want to make an application on Android to view my database from MySQL, but my code does not work properly The Android device told me that it could not "connect to the database".
This is a screenshot of my problem:
This is my This is my PHP code: I'm using XAMPP. Please help me very much for your help. You can not host local code in Android code that you get under running; CMD & gt; Right click on CMD Run ipconfig MainActivity.java code:
package com.example .bismillahirohamnirohim; Import java.io.buffferedReader; Import java.io.InputStream; Import java.io.InputStreamReader; Import org.apache.http.HttpEntity; Import org.apache.http.HttpResponse; Import org.apache.http.client.HttpClient; Import org.apache.http.client.methods.HttpGet; Import org.apache.http.client.methods.HttpPost; Import org.apache.http.impl.client.DefaultHttpClient; Import org.json.JSONArray; Import org.json.JSONObject; Android Import annotations. Import android App Import android.os.asyncTask; Importroid.os.Bundle; Import android.os.StrictMode; Import android.util.log; Import android.view.Menu; Import android.widget.TextView; @SuppressLint ("NewAP") public class extends main activity activity (/ ** is called when activity is created first * / text view result view; @ overwrite public null on @release (bundle saved instenstate) {super .net (savedinstenstate); setContentView (R.layout.activity_main); StrictMode.enableDefaults (); // STRICT MODE enabled result view = (TextView) findViewById (R.id.result); get data ();} Public zero getData () {string result = ""; InputStream isr = Null; {HttpClient httpclient = New DefaultHttpClient () Try; http Post httppost = New HTTP Post ("http: //localhost/Myfile.php"); // Your PHP Script // ADDRESS // HttpPost httppost = new / / HttpPost ("http://172.23.193.32/elift-test/myfile.php") // Your // PHP script's address is HttpResponse response = httpclient.execute (httppost); HttpEntity unit = response.getEntity (); Isr = entity.getContent ();} hold (exception e) {Log.e ("log_tag", "error in http connection" + e.toString ()); resultView.setText ("Could not connect to database"); } // Change the reply to the string {BufferedReader Reader = New BufferedReader (New InputStreamReader (isr, "iso-8859-1"), 8); Stringbilder sb = new stringbiller (); String line = faucet; While ((line = reader. Readline ())! = Null () {sb.append (row + "\ n"); } Isr.close (); Result = sb.toString (); } Hold (exception e) {Log.e ("log_tag", "error in changing result" + e.toString ()); } // parse json data {string s = ""; JSONArray jArray = New JSONArray (results); For (int i = 0; i & lt; jArray.length (); i ++) {JSONObject json = jArray.getJSONObject (i); S = s + "name:" + json.getString ("id") + "" + json.getString ("user name"); } resultView.setText (s); } Hold (exception e) {// TODO: exception Log.e ("log_tag", "error parsing data" + E.Streatin ()); }}}
& lt; Php mysql_connect ("localhost", "root", ""); mysql_select_db ("jadwal"); $ Sql1 = mysql_query ("Select * from"); if successfully (query! $ Sql1) {echo "db" could not successfully run query ($ sql) ":". Mysql_error (); Go out; } While ($ line = mysql_fetch_assoc ($ sql1)) $ output [] = $ line; Print (json_encode ($ production)); Mysql_close (); ? & Gt;
http : // (System IP Address) / MyFile.FP
Comments
Post a Comment