关于Android通过jsp连接Oracle数据库

Android客户端代码:

  1.      main.xml(布局文件):

  <?xml version="1.0" encoding="utf-8"?>

  <LinearLayout xmlns:android="http://schemas.android.com/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数据 "

  />

  <TextView

  android:id="@+id/myText"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  />

  </LinearLayout>

  2.   main.java(Activity):

  package com.AndroidLinkToJsp;

  import java.io.IOException;

  import java.io.StringReader;

  import javax.xml.parsers.SAXParserFactory;

  import org.apache.http.HttpResponse;

  import org.apache.http.client.ClientProtocolException;

  import org.apache.http.client.methods.HttpGet;

  import org.apache.http.impl.client.DefaultHttpClient;

  import org.apache.http.util.EntityUtils;

  import org.xml.sax.InputSource;

  import org.xml.sax.XMLReader;

  import com.sazhaoxun.AndroidLinkToJsp.R;

  import android.app.Activity;

  import android.os.Bundle;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  import android.widget.TextView;

  public class main extends Activity {

  private TextView myText;

  private Button myButton;

  //命名空间

  //  private static final String urlStr="http://10.88.5.225:8080/test/MyJsp.jsp";

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  myText = (TextView)findViewById(R.id.myText);

  myButton = (Button)findViewById(R.id.myButton);

  myButton.setOnClickListener(new showButton());

  }

  class showButton implements OnClickListener{

  @Override

  public void onClick(View v)

  {

  // TODO Auto-generated method stub

  System.out.println("开始获得数据");

  String uriAPI ="http://10.88.5.225:8080/test/MyJsp.jsp";

  /*建立HTTP Get联机*/

  HttpGet httpRequest = new HttpGet(uriAPI);

  try

  {

  /*发到HTTP request*/

  HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);

  /*若状态码为200 ok*/

  if(httpResponse.getStatusLine().getStatusCode() == 200)

  {

  /*取叨并应?串*/

  String strResult = EntityUtils.toString(httpResponse.getEntity());

  /*?除?余?元*/

  //strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);

  try{

  SAXParserFactory factory=SAXParserFactory.newInstance();

  XMLReader reader=factory.newSAXParser().getXMLReader();

  reader.setContentHandler(new ContentHandler());

  reader.parse(new InputSource(new StringReader(strResult)));

  }

  catch (Exception e) {

  e.printStackTrace();

  }

  System.out.println("成功获得数据");

  myText.setText(strResult.toString());

  }

  else

  {

  myText.setText("Error Response: "+httpResponse.getStatusLine().toString());

  }

  }

  catch (ClientProtocolException e)

  {

  // mTextView1.setText(e.getMessage().toString());

  e.printStackTrace();

  }

  catch (IOException e)

  {

  // mTextView1.setText(e.getMessage().toString());

  e.printStackTrace();

  }

  catch (Exception e)

  {

  //mTextView1.setText(e.getMessage().toString());

  e.printStackTrace();

  }

  /**

  * 创建一个SAXParserFactory,再用这个工厂来创建一个XMLReader,以此来读取XML

  */

  //            SAXParserFactory factory = SAXParserFactory.newInstance();

  //            XMLReader reader = factory.newSAXParser().getXMLReader();

  //

  //            //为XMLReader设置内容处理器,MyContentHandler()为具体处理的类

  //            reader.setContentHandler(new ContentHandler());

  //            //开始解析文件

  //            reader.parse(new InputSource(new StringReader(strResult)));

  //

  }

  }

  }

     解析案例:两棵树

转载于:https://www.cnblogs.com/conca/archive/2013/03/07/2948462.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值