#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Connecting to database outputting XML


    I'm trying to connect to a database with my simple android app game. The database returns a name and score in an xml format.

    The instructions I've been given is to perform a GET request on the database (with the key) and then the database will return with the xml file.

    Can anyone point me in the right direction with this? I'm new to Java and have been reading around, but am not finding anything about connecting to a database returning an xml response.

    Thank you
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0

    GET is for webservers...


    If your instructions are to use GET then you're not talking directly to a database, just to a webserver. What returns is entirely up to the designer of the website. You probably are being sent something from a database but it's the server that's running the query and passing you what returns. This may sound like splitting hairs but precise terminology is important in programming.

    You'll need to know the exact GET string to send the server, which nobody can tell you except the server Admin.

    The commonest way to deal with XML data is to use a Sax parser. You'll find examples if you Google "Android Sax". However, it's not the simplest of things to start with as a beginner. If the file is small you'll probably find it easier using string functions to look along the XML string for the name and score.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by Tramontana
    If your instructions are to use GET then you're not talking directly to a database, just to a webserver. What returns is entirely up to the designer of the website. You probably are being sent something from a database but it's the server that's running the query and passing you what returns. This may sound like splitting hairs but precise terminology is important in programming.

    You'll need to know the exact GET string to send the server, which nobody can tell you except the server Admin.

    The commonest way to deal with XML data is to use a Sax parser. You'll find examples if you Google "Android Sax". However, it's not the simplest of things to start with as a beginner. If the file is small you'll probably find it easier using string functions to look along the XML string for the name and score.
    Hi, thanks for the response. The instructions I've been given are to perform a GET request on the server URL which returns an xml document. Returned will be a name and score. Example given to us below.

    Code:
    GET https://url.server.com/scores.php?key=keyhere
    <?xml version="1.0" encoding="utf-8" ?>
    <Scores>
    <Score epoch="123456" name="jack">50</score>
    </score>
    Epoch value is the amount of seconds since a certain date.

    How would I go about making this get request, and displaying the name and score to the screen.

    Thanks!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    8
    Rep Power
    0
    This is quite a big ask, as there are several parts involved. First there's issuing the GET request. I don't have code for this but I do have a POST equivalent and most servers will accept either. The code is below; it builds a request, sends it and waits for a reply. After that you need to parse the incoming XML to find your data, and finally it goes to the screen. You'll need to do this in the UI thread; there's a standard method for this.

    If all this seems too complex for your level of Java knowledge perhaps I can offer an alternative. I'm due next week to announce a new app for programming Android; a high-level scripting system that requires no knowledge of Java or XML. I'm looking for some real-world users willing to help me by testing the functionality they need for their own apps. The HTTP POST code below is already being added. I didn't have any plans to do a Sax XML parser but would do so if it were needed for a particular project. If you're interested, go to Easy Coder then let me know.

    package <your package>;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URLDecoder;
    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;

    public class HttpSender extends Thread {

    private List<NameValuePair> data;

    public void send(String template, String content) {
    data = new ArrayList<NameValuePair>();
    data.add(new BasicNameValuePair("key", "keyhere"));
    start();
    }

    @Override
    public void run() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://url.server.com/scores.php");

    try {
    httppost.setEntity(new UrlEncodedFormEntity(data));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
    BufferedReader bufferedReader = new BufferedReader(
    new InputStreamReader(response.getEntity().getContent()));
    StringBuffer stringBuffer = new StringBuffer("");
    String line = "";
    String LineSeparator = System.getProperty("line.separator");
    while ((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line + LineSeparator);
    }
    bufferedReader.close();
    handleResponse(stringBuffer.toString().trim());
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    }

    private void handleResponse(String response) throws IOException {
    }

IMN logo majestic logo threadwatch logo seochat tools logo