user1402763
user1402763

Reputation: 21

retrieving data from php website to an Android App

Hi guys I´m pretty much a begginer in Android. This is the situation. I´m trying to write an app which basically obtains data from a website an displays it in the App, the data from the website is PHP and I´m confuse in how to do it, I heard about json, and parsing in xml but I just don´t know how to do it. What should I use to parse the data? and how?, somebody told me about AMF but I have no idea what is that, so can somebody help me with this, an example "begginer friendly" would be appreciated. Thanks guys!!

Upvotes: 2

Views: 6555

Answers (3)

roysch
roysch

Reputation: 773

First let's think about it, what will be the flow of your combined (Android + PHP) application?

  1. The Android App will call your site via REST (you put the needed parameters in the HTTP/GET request): http://mySite.org/fetchData.php?neededId=12345
  2. The PHP script fetchData.php will need to understand your request: PHP script: "ah! - I know that if I get neededId as parameter I should supply a two dimensional array of XYZ."
  3. Then your PHP script will collect the data needed and prepare it as JSON. JSON is a good choice because it is compact, unlike an XML, and there are easy ways to manipulate it (just as with an XML). If you have a rather simple data structure - I would definitely go with JSON.
  4. Then your script will send the response of the data as a stream in a form of JSON.
  5. Your Android App will receive the response, save it as a string/file and then you can parse it.

I would recommend using Google: take the points I wrote and ask Google questions about how to accomplish every one of them.

Search engine keywords:

  1. How to retrieve data + PHP + REST (I assume you know how to do this in PHP), How to use REST with Android (although this is a Java issue - how to call a URI)
  2. PHP mySQL retrieve data. (I assume you know how to do this in PHP)
  3. Create JSON with PHP
  4. ...
  5. Parse JSON in Java + Android. Here is one example: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

Always when you face a big problem (also in real life) try to:

  1. Break it down to smaller problems (this will make them seem not that scary)
  2. Think what you already know how to solve.
  3. Think about what is new to you and you will have to learn.
  4. Start with the simplest stuff - this helps moving forward and prevents frustration in an early stage.
  5. Keep cool - issues (especially in the programming world) can be always solved. It is only a matter of how much you want it. 5.

Well - so I didn't give you the answer to your questions but I believe that I showed you a possible path for a solution.

P.S. - stackoverflow is a home for many professionals (not counting myself...). They invest a lot of time and effort in their own problems and are very kind in trying to help others. Always show them that you are willing to invest efforts in solving your own problems - otherwise you might discourage them too quickly :-)

Tons of luck!

Upvotes: 4

Sparky
Sparky

Reputation: 8477

You need to clarify your question. The fact that the web site is powered by PHP is irrelevant, unless you are writing it. You have an HTTP interface to your data. You need to fetch things into your Android client using HTTP. That's the first example you need.

    URL url = null;
    HttpURLConnection urlConnection = null;

    try {
        // Fetch URL
        url = formatQuery( "this part is up to you" );
        while (url != null) {
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            parseResult( isr );
            url = null;
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    } finally {
        urlConnection.disconnect();
    }

The next question is, what format is the data in that is returned by the PHP app? Is it JSON or XML? That's not covered by your question, so I suggest you figure out which it is, and pursue the appropriate path. If you need to ask a second question, do that.

Upvotes: 2

Related Questions