How To connect android to internet Database

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

How To connect android to internet Database

Postby suharyadi » Sat Jan 05, 2008 1:37 am

any one can help me how to connect my android aplication to the database on my website :(
suharyadi
Freshman
Freshman
 
Posts: 6
Joined: Wed Dec 26, 2007 10:09 am

Top

Postby madmax » Sun Jan 06, 2008 8:18 pm

hi suharyadi,
how are you trying to connect to your db-server? via jdbc?
is you database server configured to be accessible from the whole internet???
Best Regards
madmax
Problems with the board? Just send me an Private Message or an Email: madmax at anddev.org
madmax
Site Admin
Site Admin
 
Posts: 7
Joined: Wed Nov 14, 2007 1:25 pm
Location: Germany

Postby faldureon » Sun Jan 06, 2008 11:08 pm

In my own android contest entry I use http GET and POST from the phone and a servlet with jdbc on the server. It is not necessary or even advisable to use j2ee on the server side, as it is probably too bulky, but I do it for the consistency of the server and the client domain code, for most people even cgi would be sufficient.

On the android side you need a method that writes http POSTS and GETS and retrieves data for them, in my app it looks like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public static final byte[] TERMINATING_BYTES = new byte[] { -100, -100,
  3.  
  4.                         -100, -100, -100, -100 };
  5.  
  6.  
  7.  
  8.         public static final String SERVER_NAME = "http://192.168.1.4:8080/GeocastServer/ShareablePostingServlet";
  9.  
  10.  
  11.  
  12.         private String session;
  13.  
  14.  
  15.  
  16.         @Override
  17.  
  18.         public List<Shareable> getShareables(Location myLocation) throws Exception {
  19.  
  20.                 URL url = new URL(SERVER_NAME);
  21.  
  22.  
  23.  
  24.                 List<Shareable> retval = new ArrayList<Shareable>();
  25.  
  26.                 URLConnection urlConnection = url.openConnection();
  27.  
  28.                 if (session != null) {
  29.  
  30.                         urlConnection.setRequestProperty("cookie", session);
  31.  
  32.                 }
  33.  
  34.  
  35.  
  36.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
  37.  
  38.                 InputStream inputStream = urlConnection.getInputStream();
  39.  
  40.  
  41.  
  42.                 String cookie = urlConnection.getHeaderField("Set-Cookie");
  43.  
  44.                 if (cookie != null) {
  45.  
  46.                         session = cookie.substring(0, cookie.indexOf(";"));
  47.  
  48.                 }
  49.  
  50.  
  51.  
  52.                 int temp;
  53.  
  54.                 byte[] lastXbytes = new byte[TERMINATING_BYTES.length];
  55.  
  56.                 int currentPosition = 0;
  57.  
  58.                 int beginingOfThisShareableIndex = 0;
  59.  
  60.                 while ((temp = inputStream.read()) != -1) {
  61.  
  62.                         bos.write(temp);
  63.  
  64.                         lastXbytes[currentPosition % lastXbytes.length] = (byte) temp;
  65.  
  66.  
  67.  
  68.                         if (currentPosition == 261) {
  69.  
  70.                                 System.out.println();
  71.  
  72.                         }
  73.  
  74.  
  75.  
  76.                         if (lastXbytes.length == TERMINATING_BYTES.length
  77.  
  78.                                         && Arrays.equals(lastXbytes, TERMINATING_BYTES)) {
  79.  
  80.                                 byte[] forThisShareable = new byte[currentPosition
  81.  
  82.                                                 - TERMINATING_BYTES.length
  83.  
  84.                                                 - beginingOfThisShareableIndex];
  85.  
  86.                                 System.arraycopy(bos.toByteArray(),
  87.  
  88.                                                 beginingOfThisShareableIndex, forThisShareable, 0,
  89.  
  90.                                                 forThisShareable.length);
  91.  
  92.                                 Shareable shareable = Shareable.readFromBytes(forThisShareable);
  93.  
  94.                                 retval.add(shareable);
  95.  
  96.                                 beginingOfThisShareableIndex = currentPosition;
  97.  
  98.                         }
  99.  
  100.                         currentPosition++;
  101.  
  102.                 }
  103.  
  104.  
  105.  
  106.                 return retval;
  107.  
  108.         }
  109.  
  110.  
  111.  
  112.         @Override
  113.  
  114.         public void uploadShareable(Shareable shareable) throws Exception {
  115.  
  116.                 URL url = new URL(SERVER_NAME);
  117.  
  118.  
  119.  
  120.                 URLConnection conn = url.openConnection();
  121.  
  122.                 conn.setDoOutput(true);
  123.  
  124.                 OutputStream wr = conn.getOutputStream();
  125.  
  126.  
  127.  
  128.                 wr.write(shareable.getBytes());
  129.  
  130.                 wr.flush();
  131.  
  132.  
  133.  
  134.                 conn.getInputStream();
  135.  
  136.         }
  137.  
  138.  
  139.  
  140.         @Override
  141.  
  142.         public byte[] getShareableContent(long shareableId) throws Exception {
  143.  
  144.                 URL url = new URL(SERVER_NAME + "?shareableId=" + shareableId);
  145.  
  146.  
  147.  
  148.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
  149.  
  150.                 InputStream inputStream = url.openStream();
  151.  
  152.                 int temp;
  153.  
  154.                 while ((temp = inputStream.read()) != -1) {
  155.  
  156.                         bos.write(temp);
  157.  
  158.                 }
  159.  
  160.                 return bos.toByteArray();
  161.  
  162.         }
  163.  
  164.  
  165.  
  166.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


On the server side you need a servlet that processes the requests from the phone, uses JDBC to talk to the database of your choice and sends data back to the phone. If you try to use the jdbc classes included in Android you will fail unless you find the where the Database drivers are, let me know if you find them, I looked but there weren't any.


The servlet should look similar to this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public static final byte[] TERMINATING_BYTES = new byte[] { -100, -100,
  3.  
  4.                         -100, -100, -100, -100 };
  5.  
  6.         static final long serialVersionUID = 1L;
  7.  
  8.  
  9.  
  10.         private Map<String, List<Shareable>> shareablesSentToClients;
  11.  
  12.  
  13.  
  14.         /*
  15.  
  16.          * (non-Java-doc)
  17.  
  18.          *
  19.  
  20.          * @see javax.servlet.http.HttpServlet#HttpServlet()
  21.  
  22.          */
  23.  
  24.         public ShareablePostingServlet() {
  25.  
  26.                 super();
  27.  
  28.                 shareablesSentToClients = new HashMap<String, List<Shareable>>();
  29.  
  30.         }
  31.  
  32.  
  33.  
  34.         /*
  35.  
  36.          * (non-Java-doc)
  37.  
  38.          *
  39.  
  40.          * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
  41.  
  42.          *      HttpServletResponse response)
  43.  
  44.          */
  45.  
  46.         protected void doGet(HttpServletRequest request,
  47.  
  48.                         HttpServletResponse response) throws ServletException, IOException {
  49.  
  50.                 response.resetBuffer();
  51.  
  52.  
  53.  
  54.                 HttpSession session = request.getSession();
  55.  
  56.                 String id = session.getId();
  57.  
  58.  
  59.  
  60.                 String shareableIdasString = request.getParameter("shareableId");
  61.  
  62.                 OutputStream out = response.getOutputStream();
  63.  
  64.                 WebApplicationContext context = WebApplicationContextUtils
  65.  
  66.                                 .getWebApplicationContext(this.getServletContext());
  67.  
  68.                 try {
  69.  
  70.                         if (shareableIdasString != null) {
  71.  
  72.                                 Shareable shareable = ((ShareableDao) context
  73.  
  74.                                                 .getBean("ShareableDao")).getShareableById(Long
  75.  
  76.                                                 .valueOf(shareableIdasString));
  77.  
  78.                                 out.write(shareable.getContent());
  79.  
  80.                         } else {
  81.  
  82.                                 if (shareablesSentToClients.get(id) == null) {
  83.  
  84.                                         shareablesSentToClients.put(id, new ArrayList<Shareable>());
  85.  
  86.                                 }
  87.  
  88.                                 List<Shareable> shareables = ((ShareableDao) context
  89.  
  90.                                                 .getBean("ShareableDao")).getShareables(null);
  91.  
  92.                                 // don't resend the same shareables to the same clients
  93.  
  94.                                 for (Shareable shareable : shareables) {
  95.  
  96.                                         if (!shareablesSentToClients.get(id).contains(shareable)) {
  97.  
  98.                                                 shareablesSentToClients.get(id).add(shareable);
  99.  
  100.                                                 shareable.setContent(null);
  101.  
  102.                                                 out.write(shareable.getBytes());
  103.  
  104.                                                 out.write(TERMINATING_BYTES);
  105.  
  106.                                         }
  107.  
  108.                                 }
  109.  
  110.                                 System.out.println();
  111.  
  112.                         }
  113.  
  114.                 } catch (Exception e) {
  115.  
  116.                         out.write(("There was a problem" + e.getMessage()).getBytes());
  117.  
  118.                 }
  119.  
  120.         }
  121.  
  122.  
  123.  
  124.         /*
  125.  
  126.          * (non-Java-doc)
  127.  
  128.          *
  129.  
  130.          * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
  131.  
  132.          *      HttpServletResponse response)
  133.  
  134.          */
  135.  
  136.         protected void doPost(HttpServletRequest request,
  137.  
  138.                         HttpServletResponse response) throws ServletException, IOException {
  139.  
  140.                 InputStream inputStream = request.getInputStream();
  141.  
  142.                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
  143.  
  144.                 int current;
  145.  
  146.                 while ((current = inputStream.read()) != -1) {
  147.  
  148.                         bos.write(current);
  149.  
  150.                 }
  151.  
  152.  
  153.  
  154.                 Shareable shareable;
  155.  
  156.                 try {
  157.  
  158.                         byte[] rawByteArray = bos.toByteArray();
  159.  
  160.                         shareable = Shareable.readFromBytes(rawByteArray);
  161.  
  162.                         WebApplicationContext context = WebApplicationContextUtils
  163.  
  164.                                         .getWebApplicationContext(this.getServletContext());
  165.  
  166.                         ((ShareableDao) context.getBean("ShareableDao"))
  167.  
  168.                                         .uploadShareable(shareable);
  169.  
  170.                         PrintWriter out = response.getWriter();
  171.  
  172.                         out.println(shareable.getDescription());
  173.  
  174.                 } catch (ParseException e) {
  175.  
  176.                         PrintWriter out = response.getWriter();
  177.  
  178.                         out.println("There was a problem");
  179.  
  180.                 } catch (BeansException e) {
  181.  
  182.                         PrintWriter out = response.getWriter();
  183.  
  184.                         out.println("There was a problem");
  185.  
  186.                 } catch (Exception e) {
  187.  
  188.                         PrintWriter out = response.getWriter();
  189.  
  190.                         out.println("There was a problem");
  191.  
  192.                 }
  193.  
  194.  
  195.  
  196.         }
  197.  
  198.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby suharyadi » Mon Jan 07, 2008 12:11 pm

thank's all for your help i will try it :P .
but should i configure my website Database, if should be what should be configured and how to configured it.
:?:
suharyadi
Freshman
Freshman
 
Posts: 6
Joined: Wed Dec 26, 2007 10:09 am

Postby faldureon » Tue Jan 08, 2008 4:20 am

I am a minimalist when it comes to programming so I use Mysql 5 and the geo-spacial extensions that come with it. You shouldn't need to do any setup beyond downloading/installing and creating tables. After that you just download the mysql jdbc driver, add it to your classpath and use jdbc like always.
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby patrox » Sun Jul 06, 2008 6:56 pm

faldureon wrote:I am a minimalist when it comes to programming so I use Mysql 5 and the geo-spacial extensions that come with it. You shouldn't need to do any setup beyond downloading/installing and creating tables. After that you just download the mysql jdbc driver, add it to your classpath and use jdbc like always.


does that really work?

i think jdbc is not working on android , have you made it work?
patrox
Freshman
Freshman
 
Posts: 8
Joined: Wed Apr 02, 2008 8:21 pm

Top

Postby faldureon » Sun Jul 06, 2008 9:42 pm

Jdbc does not work on android yet, the classes are there, but there are no drivers for any of the databases.

In order to use a fully functional relational database on an android device you must create a web server that uses a traditional version of java and connects to a db, and then communicates with the android device over http. This actually ends up being much tidier since it forces you to separate persistence logic from view logic. It does take more time/work to implement though
faldureon
Developer
Developer
 
Posts: 27
Joined: Sun Dec 02, 2007 10:19 pm

Postby patrox » Mon Jul 07, 2008 7:50 am

yeah i learnt about it that weekend....
and im disappointed cause i ve designed my university project thinking there are jdbc drivers.
its back to servlets now i guess.
patrox
Freshman
Freshman
 
Posts: 8
Joined: Wed Apr 02, 2008 8:21 pm

connection between DB server and android app

Postby wajdi » Sat Jul 12, 2008 6:17 pm

faldureon wrote:Jdbc does not work on android yet, the classes are there, but there are no drivers for any of the databases.

In order to use a fully functional relational database on an android device you must create a web server that uses a traditional version of java and connects to a db, and then communicates with the android device over http. This actually ends up being much tidier since it forces you to separate persistence logic from view logic. It does take more time/work to implement though


could you plzzz give a hand to establish a connection and send/receive data and requests between my website DataBase and my application, i am new in this field but i will be greatful if there are any useful links or tutorials wich help.

thanksssssssss !!!!
wajdi
Freshman
Freshman
 
Posts: 4
Joined: Wed Jul 02, 2008 10:10 am

Postby m.francia » Wed Mar 24, 2010 2:22 pm

patrox wrote:yeah i learnt about it that weekend....
and im disappointed cause i ve designed my university project thinking there are jdbc drivers.
its back to servlets now i guess.


damn....me too!

or....the post is quite old...maybe now the jdbc drivers work...or not?
Image
User avatar
m.francia
Freshman
Freshman
 
Posts: 5
Joined: Wed Mar 24, 2010 2:11 pm

Top

Return to General

Who is online

Users browsing this forum: No registered users and 2 guests