views:

195

answers:

2

hi

I am new to android development. how to get the data from the struts application database. I want list out the records from the server database to android.

reply me quickly

+1  A: 

just expose a rest, or similar api from your struts application, you can then use this api via android to retrieve the data you want.

broschb
our application is connect to the database through hibernate
saravanan
A: 

Use This code to send data from your android to struts2 action and get the result back

package MY PACKAGE;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;



public class AndroidClient{

    private URL m_cURL = null;
    private HttpURLConnection m_cConn = null;




  public void connectToServer(){
        try {
            m_cURL = new URL("http://YOURIP:8084/MyAction");
            if(null == m_cConn){
                m_cConn = (HttpURLConnection) m_cURL.openConnection();
                m_cConn.setDoOutput(true);
                m_cConn.setDoInput(true);
            }
        }catch (Exception e) {
            System.out.println("Connection Error::"+e);
        }
    }

    public String fetchUserDetails(String userID){
        DataOutputStream dos = null;
        InputStream is = null;
        String responseUserDetails = null;
        try {
            connectToServer();

            String uID="yourId";
            dos = new DataOutputStream(m_cConn.getOutputStream());
            dos.write(uID.getBytes(), 0, uID.getBytes().length);

            is = m_cConn.getInputStream();
            StringBuilder sb = new StringBuilder();
            String line;

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            responseUserDetails = sb.toString();
            System.out.println("Response Text(Fetch): " + responseUserDetails);

        } catch (Exception e) {
            return responseUserDetails;
        } finally{
                try{
                    if (is != null) 
                        is.close(); 
                    if (dos != null){
                        dos.flush();    
                        dos.close();
                    }
                    disconnectServer();
                }catch(Exception pEx){
                    System.out.println(pEx.toString());
                return responseUserDetails;
            }
        }
        return responseUserDetails;
    }

    public void disconnectServer(){
        try {
            m_cConn.disconnect();
            m_cConn = null;
        }catch (Exception e) {
            System.out.println("Connection Error::"+e);
        }
    }
}
Vinayak.B
thank you for your code. i want to connect my web application mysql server datas from android emulator please reply me
saravanan
In android you can connect to SQLite Database that comes built in with android, If you want to query server database then you query it in your action class and send the query result to the android code and use it has you can.
Vinayak.B
how to send the query our server database. please give me sample code
saravanan