moesef
moesef

Reputation: 4841

Get data from PHP script in JAVA

I am fairly new to Android programming and was wondering how I can get data from an SQL database in my Android app.

I currently have a PHP script that pulls the data I want from the SQL table but I'm not sure how to pull the data from the PHP script into my Java. How do I do this? I read something about SOAP. Is this the protocol I want to use?

Thanks

Upvotes: 0

Views: 2517

Answers (2)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

It depends. Where's the database, on the device, or on a server? If it's on the server, is the PHP code already a web app, or is it just a script?

In general, if the database is on the device, throw out the PHP and use JDBC to grab the data directly from Java. If the data is on the server, then turn the PHP script into a web app, and access that web app from Java. SOAP is certainly one protocol you can use for this, albeit a complex one that's often overkill. JSON or just plain text are many times better choices.

Upvotes: 1

You can use the function below to get content from your PHP script

    public static String get(String from) {
        try {
            HttpClient client = new DefaultHttpClient();  
            HttpGet get = new HttpGet(from);
            HttpResponse responseGet = client.execute(get);  
            HttpEntity resEntityGet = responseGet.getEntity();  
            if (resEntityGet != null) return EntityUtils.toString(resEntityGet);
        } catch (Exception e) {
            Log.e("", e.toString());
        }
        return null;
    }

Upvotes: 0

Related Questions