1

I've created basic android apps in various programming classes that I have taken before using Eclipse and the Java Android SDK. The app that I'd like to create would require users to enter information that would later be analyzed. I want people to be able to compare this data with other people's data so I'd like every entry that users make to be submitted to a database to later be queried when a person attempts to compare their data. I'd like direction for how to accomplish this. Should I find a free hosting site and set up a Sql server or is there a better way to accomplish this?

Edit: Just for fun.

Reed B
  • 606
  • 1
  • 8
  • 18

3 Answers3

2

I am a very beginner android developer, and I have found that using cloud-stored online database like mongolab.com is very friendly for user submitted data. The communication between database and server will have to be done through JSON parsing and URI requests.

Here is example of code you can bind to a button that will send object stored in field tempData:

public void send(View view) {
    String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA?apiKey="
        + apiKey;
    try {

      // make web service connection
      final HttpPost request = new HttpPost(apiURI);
      request.setHeader("Accept", "application/json");
      request.setHeader("Content-type", "application/json");

      // Build JSON string with GSON library
      Gson gson = new Gson();

      JsonElement jsonElement = gson.toJsonTree(tempData);
      String json = gson.toJson(jsonElement);
      StringEntity entity = new StringEntity(json);

      Log.d("****Parameter Input****", "Testing:" + json);
      request.setEntity(entity);
      // Send request to WCF service
      final DefaultHttpClient httpClient = new DefaultHttpClient();

      new AsyncTask<Void, Void, Void>() {
        @Override
        public Void doInBackground(Void... arg) {
          try {
            HttpResponse response = httpClient.execute(request);
            Log.d("WebInvoke", "Saving: "
                + response.getStatusLine().toString());
            // Get the status of web service
            BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity()
                    .getContent()));
            // print status in log
            String line = "";
            while ((line = rd.readLine()) != null) {
              Log.d("****Status Line***", "Webservice: " + line);

            }
          } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
          }
          return null;
        }
      }.execute();

    } catch (Exception e) {
      e.printStackTrace();
    }

  }

And here is an example of code used to retrieve elements in the database:

public void load() {
    String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA"
        + "?apiKey=" + apiKey;

    Log.d("****Status Line***", "" + apiURI);

    try {

      // make web service connection
      final StringBuilder builder = new StringBuilder();

      final HttpGet request = new HttpGet(apiURI);
      request.setHeader("Accept", "application/json");
      request.setHeader("Content-type", "application/json");
      final DefaultHttpClient httpClient = new DefaultHttpClient();

      new AsyncTask<Void, Void, String>() {

        @Override
        protected void onPostExecute(String result) {
          super.onPostExecute(result);
          doSomethingWithReceivedData(result); //THIS METHOD IS DEFINED IN BODY OF YOUR ACTIVITY
        }

        @Override
        public String doInBackground(Void... arg) {
          try {
            HttpResponse response = httpClient.execute(request);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {

              HttpEntity entity = response.getEntity();
              InputStream content = entity.getContent();

              BufferedReader reader = new BufferedReader(
                  new InputStreamReader(content));
              String line;
              while ((line = reader.readLine()) != null) {
                builder.append(line);
              }
              Log.d("****Status Line***", "Success");

              return builder.toString();

            } else {
              Log.d("****Status Line***",
                  "Failed to download file");
            }

          } catch (Exception e) {
            Log.e("SendMail", e.getMessage(), e);
          }
          return null;
        }
      }.execute();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
LucasSeveryn
  • 5,252
  • 8
  • 32
  • 58
  • 1
    +1 nice answer. @Reed B : If [Redis](http://redis.io/) is more to your taste: [RedisCloud](http://redis-cloud.com/). – Fildor Jun 05 '13 at 16:27
  • I tried to answer as I want to see my questions answered, I find it much easier to reverse engineer working examples than fighting through documentations. – LucasSeveryn Jun 05 '13 at 16:31
  • Thanks for the help! Since this is a casual project, I will try to go with a free webhost like 000webhost. – Reed B Jun 05 '13 at 16:58
0

You should have a data base to store the data. Like mentioned above, the data base is good to be in MySQL (SQL). Your application should have a method that can POST the results to the server, where the server will read the string send and retrieve and store the data. A good start is to read about JSON and read also about Asynctask

Also you need to know how to build your sever part. A good idea is to start with PHP, but I am not an expert on that field. I hope this helps you start your project.

Community
  • 1
  • 1
malcolm the4
  • 307
  • 2
  • 5
  • 15
  • I am not saying that php is mandatory. Programming the server is something different than programming the android app. The server can be implemented in many ways, maybe he can do it also in JAVA or JSP... – malcolm the4 Jun 05 '13 at 16:18
  • 1
    Well, I don't even see PHP as a good starting point. A better one would be to find free databases that work with CRUD APIs, so you don't need to write a server. For a beginner, it should be as easy as possible. He shouldn't have to deal with both side of the force. – Fildor Jun 05 '13 at 16:22
0

Simple, no DB required.

Usergrid by Apigee is exactly what you are looking for!

  1. You can store each user's details
  2. Retrieve stored data
  3. Send events and receive event callbacks across devices

Best of all - no server side code. Only APIs

FYI This is the direction you should be heading even if you know how to code a server.

PS: I don't work for apigee or usergrid.

Community
  • 1
  • 1
Dheeraj Bhaskar
  • 17,151
  • 9
  • 58
  • 65