Android Open Source - Points Storage






From Project

Back to project page Points.

License

The source code is released under:

GNU General Public License

If you think the Android project Points listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package se.frusunnanbo.points;
/*from   w w  w  .j  a  va  2  s .  c o m*/
import java.util.Date;
import java.util.Locale;

import android.content.Context;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;

import android.util.Log;

public class Storage {
  
  private Prefs prefs;
  private static final String TAG = "Storage";
  
  public Storage(Prefs prefs) {
    this.prefs = prefs;
  }
  
  private String sendDataToServer(String postBody) throws IOException {
    String response = "";
    
    try {
      URL url = new URL("http://" + prefs.getUri() +  "achievement");  
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      byte[] buffer = new byte[100];
      
      try {
        connection.setDoOutput(true);
      
        OutputStream out = new BufferedOutputStream(connection.getOutputStream());
        out.write(postBody.getBytes());
        out.flush();

        if (connection.getResponseCode() != 200) {
          response = "Error! Response code: " + connection.getResponseCode();
        }
        InputStream in = new BufferedInputStream(connection.getInputStream());
        while (in.read(buffer) != -1) {
          response += new String(buffer);
        }
      }
      finally {
        connection.disconnect();
      }

    }
    catch (IOException e) {
      Log.e(TAG, "Connection/ communication error: " + e.toString());
      throw e;
    }    
    
    return response;
  }
  
  public void save(Context context, Date date, int task1points, int task2points) throws IOException {
    DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, new Locale("sv", "SE"));
    
    String save1String = "childName=" + prefs.getChildName() + 
                      "&parentName=" + prefs.getParentName() + 
                    "&achievementType=" + prefs.getTask1Name() + 
                    "&earnedMoney= " + task1points + 
                    "&achievementDate=" + df.format(date);
    
    String save2String = "childName=" + prefs.getChildName() + 
              "&parentName=" + prefs.getParentName() +  
            "&achievementType=" + prefs.getTask2Name() + 
            "&earnedMoney= " + task2points + 
            "&achievementDate=" + df.format(date);
    
    sendDataToServer(save1String);
    sendDataToServer(save2String);
  }

}




Java Source Code List

se.frusunnanbo.points.Constants.java
se.frusunnanbo.points.DailyAlarmStarter.java
se.frusunnanbo.points.DailyPointsActivity.java
se.frusunnanbo.points.Prefs.java
se.frusunnanbo.points.SettingsActivity.java
se.frusunnanbo.points.Storage.java