Android : Parsing response of Axis 2 web Service in Android

on Wednesday, July 9, 2014


I am using KSOAP2 to call a Axis 2 webservice from android application,and the response from the web service is in the following format:



[Book{id=1; name=C++ for Begginers; Author=Martin;},Book{id=2; name=Java Development; Author=Charles;},Book{id=3; name=Android Guide; Author=Sam};]


The code for Axis 2 Web service class is as following:



public class Book_Web_Service {
private String url;
private Connection con;
private Statement stmt;

public void connectToDB() {
url = "jdbc:mysql://localhost:3306/book";
try {
Class.forName("com.mysql.jdbc.Driver");
con = (Connection) DriverManager.getConnection(url, "root", "");
} catch (Exception e) {
System.out.println("Error - Unable to Connect to the Database" + e);

}
}

public ArrayList<Book> getBooksData() {
ArrayList<Book> booklist = new ArrayList<Book>();
connectToDB();

try {
stmt = (Statement) con.createStatement();

String query ="SELECT * from book ";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {

Book b = new Book();
b.setId(rs.getString("id"));
b.setName(rs.getString("name"));
b.setAuthor(rs.getString("author"));

booklist.add(b);
}
}

catch (SQLException e) {
System.out.println("Error - Unable to get books data ......" + e);
}
return booklist;
}
}


The code for getting response in android is this:



ArrayList<Book> booklist = new ArrayList<Book>();


METHOD_NAME = "getBooksData";
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Object result = envelope.getResponse();


// SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String response = result.toString();


((TextView) findViewById(R.id.booktxt)).setText("Response:"+ response);
} catch (Exception E) {
E.printStackTrace();
((TextView) findViewById(R.id.booktxt)).setText("ERROR:"
+ E.getClass().getName() + ":" + E.getMessage());
}


I want the response again be in arraylist. So that I can show it in list view. But I am not getting it. Is there any android library which can do this or any easy way to do it????


0 comments:

Post a Comment