android sql data
I am new to android and I want to communicate with the server to transfer sql data. I have read posts using raw http and also referred Unlocking Android chapter 6. But are there more 开发者_StackOverflow社区clearer steps? Like where should I start my reading and what examples I should be looking at?
My end goal is to send and receive data from a sql database on a online server. I know i have to use a medium like php to perform query but my confusion is more on the android side.
Well there are alot of way you could accomplish this. Here is one.
- Write a backend in PHP (or the language of your choice) to run SQL queries and then take that data and serialize it into JSON.
- Then in the Android app request the Url of the PHP file.
- Use GSON or another JSON parser to parse the JSON into objects.
- Display the object on App (List, Map ...)
If you are having trouble with the Android side of it I would recommend looking at some documentation.
EDIT Code for building a url using a map where the keys are the get parameters and the values are the value of the get parameters.
if (vars != null) {
Iterator<String> itr = vars.keySet().iterator();
boolean first = true;
// Append them to the url
while (itr.hasNext()) {
String key = itr.next();
if (first) {
builder.append("?" + key + "=" + vars.get(key));
first = false;
} else {
builder.append("&" + key + "=" + vars.get(key));
}
}
}
builder.toString();
精彩评论