user3006788
user3006788

Reputation: 165

How to connect my Android app to my PHP/MySQL backend?

I have two questions about the connection between Android and PHP/MySQL.

  1. If I used version 3 and above is that true that I need to do the connection in background using a separate thread?

  2. Is it necessary to use JSON to get back the answers?

I wrote code without using multi-threading and JSON but it only works on the version of 2.3 and above. I tried on the 4.0 and 4.2, but it didn't give back any response.

Upvotes: 1

Views: 4200

Answers (1)

Robin Kanters
Robin Kanters

Reputation: 5146

Your first question:

Yes. Always do network tasks or anything else that takes time in the background. The best way to do this would be to use an AsyncTask. This article explains AsyncTask way better than I can, go read it.

Contrary to what the comments on your question say, the reason you should use a separate thread is not because you'd get a NetworkOnMainThreadException otherwise. It's because it's a better practice and because it makes sure your app doesn't stutter while it does the network task. The main task also handles animations etc. in your Activity, so doing any task on the main thread for X time, means the app stutters for X time.

Your second question:

No, it isn't necessary to use JSON. You do want to route your requests through a script on your webpage though (whether that'd be PHP, Ruby, Python, whatever), instead of interfacing with your database directly. This way, you can restrict the actions your app is able to perform, as well as the actions a potential hacker is able to perform.

Like I said, it is not necessary to use JSON. However, it's the most widely accepted way to get information from your server to your app, for a couple of reasons. The most prevalent 2 being:

  1. Low overhead: JSON uses very little 'extra' characters between your data, as opposed to, for example, XML, which has long tags, et cetera;
  2. Ease of use: Android has JSON tools built in for you to leverage, which makes it very easy for you to work with JSON. For example, take this bit of JSON:

[{'id':11,'name':'Bob'},{'id':42,'name':'Sally'}]

To parse this in your Android app, you could do:

public List<Person> parseJson(String jsonString) {

    // Initialize the ArrayList we're gonna store the people in
    List<Person> people = new ArrayList<Person>();

    try {
        // Convert the JSON from text (String) to a JSON Array, so we can
        // more easily traverse it
        JSONArray rootArray = new JSONArray(jsonString);

        // loop through the prople in the JSON Array
        for(int i=0; i<rootArray.length(); 

            // Get the object at position i from the JSON Array
            JSONObject workingObj = rootArray.get(i);

            // Do what you have to to store the data. In this example,
            // I'm using a class called 'Person' which has setters for Id and Name
            Person p = new Person();

            // Get all the info you need from the JSON Object. As you can see
            // in the JSON snippet, we have an integer with key 'id' and a
            // string with key 'name'
            p.setId(workingObj.getInt("id"));
            p.setName(workingObj.getString("name"));

            // add the Person p to the ArrayList
            people.add(p);
        }
    } catch (JSONException e) {
        // properly handle all exceptions!
    }
    return people;
}

As you can see, all parsing is done for you, and you just have to accommodate for the data structure.

Upvotes: 7

Related Questions