在Android中解析Axis 2 Web Service的响应

时间:2014-07-10 04:40:28

标签: java android web-services axis2

我正在使用KSOAP2从Android应用程序调用Axis 2 webservice,来自Web服务的响应采用以下格式:

[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};]

Axis 2 Web服务类的代码如下:

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

在android中获取响应的代码是:

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

我希望响应再次出现在arraylist中。这样我就可以在列表视图中显示它。但我没有得到它。是否有任何安卓库可以做到这一点或任何简单的方法来做到这一点????

0 个答案:

没有答案