0

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 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.

1 Answer 1

1

Well there are alot of way you could accomplish this. Here is one.

  1. 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.
  2. Then in the Android app request the Url of the PHP file.
  3. Use GSON or another JSON parser to parse the JSON into objects.
  4. 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();
Sign up to request clarification or add additional context in comments.

4 Comments

I did the steps above and was able to get data from sql and display it on my app using "JSONObject.getString". But i am not able to figure out how i can post data to php so that php can insert a new row -> i have tried name value pairs and httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); and i am using $_POST in php but i get NULL as the result So if you cud help me in inserting a row into the table it would be great Thank you
I usually use the GET instead of post. It is easier. You can just build the url using a StringBuilder.
i have to add an whole row .. building all of that in the url ... just thinking if it is the best way to do it?
i added some code to the answer on how to generate a url. I found that maps are the best way using the key as the GET parameter name and the value as the GET parameter value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.