Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are various posts in SO relating to PHP integration with Android apps. You want to search for Android JSON. That's how I was able to link my app to a MySQL database on the web. PostqreSQL should be no different, except maybe the PHP query calls.</p> <p>What you'll need to do is write a PHP program that connects to your PSQL database and runs a query on it. The query then needs to be formatted into a JSON array. My sample below connects to a mysql database. Since I have never used PostgreSQL, I am not sure what the PHP code to query it is. You should be able to find that info at PHP.net though.</p> <pre><code>&lt;?php $db = mysql_connect('localhost','mydatabase', 'mypassword'); mysql_select_db("myDB"); $TABLE = isset($_POST["table"]) ? $_POST["table"] : "rep_table"; $q = mysql_query("SELECT * FROM " . $TABLE); while($row=mysql_fetch_assoc($q)) { $result[]=$row; } print(json_encode($result)); mysql_close(); ?&gt; </code></pre> <p>Your app would then make a call to this PHP file through a JSON interface, which you can then parse and get the values out of.</p> <pre><code>public class JSONHelper { static ArrayList&lt;NameValuePair&gt; mArray = new ArrayList&lt;NameValuePair&gt;(); private static final String TAG = "JSONUpdater"; public String getJSONResult(String url) { String result = ""; InputStream is = null; try { HttpClient httpclient = new DefaultHttpClient(); Log.d(TAG, "Client set"); HttpPost httpPost = new HttpPost(url); Log.d(TAG, "Post set"); if (!mArray.isEmpty()) { Log.d(TAG, "mArray is not empty!"); httpPost.setEntity(new UrlEncodedFormEntity(mArray)); } else { Log.d(TAG, "mArray is SO empty!!"); } HttpResponse response = httpclient.execute(httpPost); Log.d(TAG, "response executed"); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch(Exception e) { Log.d("ScoreCard",e.toString()); } //Retrieve the JSON data try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { Log.d(TAG, line); sb.append(line + "\n"); } is.close(); result=sb.toString(); }catch(Exception e){ Log.e("Scorecard", "Error converting result "+ e.toString()); return result; } //Hopefully returning a string that can be converted to a JSON array... return result; } } </code></pre> <p>Don't want to overload you with info, because it can be overwhelming. Try this out as a starting point and see if you can get to where you need. If not, let me know. I'll try to help you out if I can.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload