Situation:
You got some (client) application, that needs data from a mysql database.
The application cannot connect to the database itself. It sends a HTTP GET/POST request to a webserver, that gets the data out of the mysql database and packs it into a JSON Array. This JSON Array is sent back to the client.
The client receives the JSON Array and parses it.
This is the php code for the webserver (inspired from )
/**
* download list of registered users from server at cmd.eyedbits.com
*
* @return list of users
*/
static Vector<String> downloadUserList() {
Log.i(TAG, String.format("Downloading UserList"));
String serverUrl = SERVER_URL + SERVER_URL_QUERYUSERLIST;
//TODO: implement multiple tries with exponential backoff if server is not responding
DefaultHttpClient hc = new DefaultHttpClient();
ResponseHandler <String> res = new BasicResponseHandler();
HttpGet getMethod = new HttpGet(serverUrl);
Vector<String> retVector = new Vector<String>();
try {
String response = hc.execute(getMethod, res);
Log.i(TAG, "Http response: " + response);
// todo check if successfull and not error
// parse string
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject row = jsonArray.getJSONObject(i);
String userName = row.getString("UserName");
retVector.add(userName);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retVector;
}