From 819197b2305a94ab4924c5ae37a3fad762078448 Mon Sep 17 00:00:00 2001 From: Petru Trimbitas Date: Thu, 23 May 2013 18:55:16 +0300 Subject: [PATCH] Add function to add event into local database --- .classpath | 1 + project.properties | 1 - src/ro/ieval/unical/DatabaseInteract.java | 20 +++++++++++++++++++- src/ro/ieval/unical/Event.java | 2 +- src/ro/ieval/unical/MySQLiteHelper.java | 8 ++++---- src/ro/ieval/unical/mainActivity.java | 23 ++++++++++++++++++++--- 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index a4763d1..bb0c759 100644 --- a/.classpath +++ b/.classpath @@ -4,5 +4,6 @@ + diff --git a/project.properties b/project.properties index 21e5f60..a3ee5ab 100644 --- a/project.properties +++ b/project.properties @@ -12,4 +12,3 @@ # Project target. target=android-17 -android.library.reference.1=../gridlayout_v7 diff --git a/src/ro/ieval/unical/DatabaseInteract.java b/src/ro/ieval/unical/DatabaseInteract.java index a4d2580..dd2dde1 100644 --- a/src/ro/ieval/unical/DatabaseInteract.java +++ b/src/ro/ieval/unical/DatabaseInteract.java @@ -1,5 +1,6 @@ package ro.ieval.unical; +import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; @@ -7,6 +8,12 @@ import android.database.sqlite.SQLiteDatabase; public class DatabaseInteract { private SQLiteDatabase database; private MySQLiteHelper dbHelper; + private String[] allColumns = { + MySQLiteHelper.ID, MySQLiteHelper.title, + MySQLiteHelper.description, MySQLiteHelper.date, + MySQLiteHelper.duration, MySQLiteHelper.repeat, + MySQLiteHelper.repeatIn, MySQLiteHelper.location + }; public DatabaseInteract(Context context) { dbHelper = new MySQLiteHelper(context); @@ -20,7 +27,18 @@ public class DatabaseInteract { dbHelper.close(); } - //Add event + //Insert event + public void insertEvent(Event e) { + ContentValues values = new ContentValues(); + values.put(allColumns[1],e.title); + values.put(allColumns[2],e.description); + values.put(allColumns[3],e.date); + values.put(allColumns[4],e.duration); + values.put(allColumns[5], e.repeat); + values.put(allColumns[6], e.repeatInterval); + values.put(allColumns[7], e.location); + database.insert(MySQLiteHelper.Name, null, values); + } //Delete event //Search event } diff --git a/src/ro/ieval/unical/Event.java b/src/ro/ieval/unical/Event.java index 8d99947..d508201 100644 --- a/src/ro/ieval/unical/Event.java +++ b/src/ro/ieval/unical/Event.java @@ -1,6 +1,6 @@ package ro.ieval.unical; public class Event { - public int date,time; + public int date,duration; public boolean repeat; public int repeatInterval; public String location; diff --git a/src/ro/ieval/unical/MySQLiteHelper.java b/src/ro/ieval/unical/MySQLiteHelper.java index 889ebda..dc35951 100644 --- a/src/ro/ieval/unical/MySQLiteHelper.java +++ b/src/ro/ieval/unical/MySQLiteHelper.java @@ -12,10 +12,10 @@ public class MySQLiteHelper extends SQLiteOpenHelper { public static final String Name="Events"; public static final String ID = "_id"; - public static final String name = "Name"; + public static final String title = "Title"; public static final String description = "Description"; public static final String date = "Date"; - public static final String time = "Time"; + public static final String duration = "Duration"; public static final String repeat = "repeat"; public static final String repeatIn = "repeatInterval"; public static final String location = "Location"; @@ -24,10 +24,10 @@ public class MySQLiteHelper extends SQLiteOpenHelper { private static final String Database_create = "create table " + Name + "(" + ID + " integer primary key autoincrement, " + - name + " text not null, " + + title + " text not null, " + description + " text, " + date + " integer, " + - time + " integer, " + + duration + " integer, " + repeat + " integer, "+ repeatIn + " integer, " + location + " text);" diff --git a/src/ro/ieval/unical/mainActivity.java b/src/ro/ieval/unical/mainActivity.java index eba4604..5edc18b 100644 --- a/src/ro/ieval/unical/mainActivity.java +++ b/src/ro/ieval/unical/mainActivity.java @@ -2,11 +2,15 @@ package ro.ieval.unical; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Date; +import javax.net.ssl.HttpsURLConnection; + import ro.ieval.unical.R; import com.google.gson.Gson; @@ -25,12 +29,23 @@ public class mainActivity extends android.app.Activity { InputStreamReader in; try { hostUrlJson=new URL(Constants.searchUrl); - URLConnection calConnection = hostUrlJson.openConnection(); - calConnection.setRequestProperty("Authorization", "Basic "+Base64.encodeToString( (Constants.user+':'+Constants.password).getBytes(), Base64.NO_WRAP)); + HttpsURLConnection calConnection = (HttpsURLConnection) hostUrlJson.openConnection(); + calConnection.setDoOutput(true); + calConnection.setUseCaches(false); + //calConnection.setRequestProperty("Authorization", "Basic "+Base64.encodeToString( (Constants.user+':'+Constants.password).getBytes(), Base64.NO_WRAP)); + //calConnection.setRequestMethod("POST"); + //calConnection.setDoInput(true); + //calConnection.connect(); + + final PrintWriter ps=new PrintWriter(calConnection.getOutputStream()); + ps.print("{}"); + ps.close(); + in = new InputStreamReader(calConnection.getInputStream()); Gson gson=new Gson(); events=gson.fromJson(in,Event[].class); in.close(); + return events; } catch (MalformedURLException e) { // TODO Auto-generated catch block @@ -53,7 +68,7 @@ public class mainActivity extends android.app.Activity { final TextView date=(TextView) findViewById(R.id.date); final TextView description=(TextView) findViewById(R.id.description); super.onCreate(savedInstanceState); - System.err.println("MuieLaComisie"); + final Handler h=new Handler(); new Thread(new Runnable() { public void run() { @@ -64,6 +79,8 @@ public class mainActivity extends android.app.Activity { eventTitle.setText(events[0].title); date.setText((new Date(events[0].date)).toString()); description.setText(events[0].description); + //DatabaseInteract db=new DatabaseInteract(null); + //db.insertEvent(events[0]); } }); } -- 2.39.2