//首先寫一個jsp后臺服務(wù)連接到Oracle數(shù)據(jù)庫的實例myOra1(此處只是測試,所以用system身份連接到該實例)
//然后再通過jsp把數(shù)據(jù)返回給Android手機客戶端
//運行jsp代碼之前必須導(dǎo)入Oracle數(shù)據(jù)庫的jdbc驅(qū)動包(jar包),名字為:classes12.jar.這個包在Oracle的安裝目錄下可已
//找到,在瀏覽器中打開jsp網(wǎng)頁前必須保證Tomcat已經(jīng)正確啟動。
//jsp取到的數(shù)據(jù)以xml格式展現(xiàn)在web頁面中
//Oracle數(shù)據(jù)庫中的表如下:

//jsp代碼:
下圖為jsp后臺取出數(shù)據(jù)的結(jié)果:

//當(dāng)jsp后臺從Oracle數(shù)據(jù)庫取到數(shù)據(jù)后就應(yīng)該返回給Android,這樣就實現(xiàn)了Android客戶端間接獲得Oracle中的數(shù)據(jù)
Android客戶端代碼:
1. main.xml(布局文件):
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas./apk/res/android"
-
- android:orientation="vertical"
-
- android:layout_width="fill_parent"
-
- android:layout_height="fill_parent"
-
- >
-
- <Button
-
- android:id="@+id/myButton"
-
- android:layout_width="wrap_content"
-
- android:layout_height="wrap_content"
-
- android:text="獲取Oracle數(shù)據(jù) "
-
- />
-
- <TextView
-
- android:id="@+id/myText"
-
- android:layout_width="fill_parent"
-
- android:layout_height="fill_parent"
-
- />
-
- </LinearLayout>
2. main.java(Activity):
3 . ContentHandler.java:
- package com.AndroidLinkToJsp;
-
-
-
- import org.xml.sax.Attributes;
-
- import org.xml.sax.SAXException;
-
- import org.xml.sax.helpers.DefaultHandler;
-
-
-
- public class ContentHandler extends DefaultHandler{
-
- String ID,NAME,AGE,SEX;
-
- String tagName;
-
-
-
- /**
-
- * 開始解析xml
-
- * @throws SAXException
-
- */
-
- public void startDocument() throws SAXException{
-
- System.out.println("--------begin--------");
-
- }
-
-
-
- /**
-
- * 結(jié)束解析xml
-
- * @throws SAXException
-
- */
-
- public void endDocument() throws SAXException{
-
- System.out.println("--------end-----------");
-
- }
-
-
-
- /**
-
- * 開始解析元素屬性
-
- *
-
- * @param namespaceURI 命名空間,防止命名重復(fù)
-
- * @param localName 不帶前綴的名字
-
- * @param qName 帶前綴的名字
-
- * @param attr 代表標簽里所有的屬性
-
- * @throws SAXException
-
- */
-
- public void startElement(String namespaceURI, String localName,
-
- String qName, Attributes attr) throws SAXException{
-
- //System.out.println("qName-------->"+qName);//調(diào)試用
-
- tagName = localName;//把當(dāng)前正在解析的無前綴的名字傳給tagName
-
- if(localName.equals("TONGXIN081")){
-
- for (int i = 0; i < attr.getLength(); i++) {
-
- System.out.println(attr.getLocalName(i) + "=" + attr.getValue(i));//得到第i個屬性的名字和值
-
- }
-
- }
-
-
-
- }
-
-
-
- /**
-
- * 結(jié)束元素解析
-
- *
-
- * @param namespaceURI
-
- * @param localName
-
- * @param qName
-
- * @throws SAXException
-
- */
-
- public void endElement(String namespaceURI, String localName, String qName) throws SAXException{
-
- //在worker標簽解析完之后,會打印出所有得到的數(shù)據(jù)
-
- //tagName = "";
-
- if(localName.equals("TONGXIN081"));
-
- this.printout();
-
- }
-
- /**
-
- * 具體解析標簽里的內(nèi)容
-
- *
-
- * @param ch 所有讀取到的內(nèi)容,都會放到char[]類型的數(shù)組里
-
- * @param start 讀取的內(nèi)容是從char[]數(shù)組的哪一位開始
-
- * @param length 從start開始,一共有多長的內(nèi)容
-
- * @throws SAXException
-
- */
-
- public void characters(char[] ch, int start, int length)
-
- throws SAXException
-
- {
-
- if (tagName.equals("ID"))
-
- ID = new String(ch, start, length);
-
- else if (tagName.equals("NAME"))
-
- NAME = new String(ch, start, length);
-
- else if (tagName.equals("AGE"))
-
- AGE = new String(ch, start, length);
-
- else if (tagName.equals("SEX"))
-
- SEX = new String(ch, start, length);
-
- }
-
- private void printout()
-
- {
-
- System.out.print("ID: ");
-
- System.out.println(ID);
-
- System.out.print("NAME: ");
-
- System.out.println(NAME);
-
- System.out.print("AGE: ");
-
- System.out.println(AGE);
-
- System.out.print("SEX: ");
-
- System.out.println(SEX);
-
- System.out.println();
-
- }
-
- }
//最后在Manifest文件中還必須聲明訪問英特網(wǎng)的權(quán)限(如下圖所示):

//下圖為Android取到j(luò)sp后臺xml格式的數(shù)據(jù)后返回給Android客戶端

|