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
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
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.
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);
}
}
}