一、Server端-JSP
<?xml version="1.0" encoding="utf-8"?>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@page contentType="text/html;charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
try
{
String x=request.getParameter("x");
String y=request.getParameter("y");
x= new String(symptom.getBytes("ISO8859_1"),"utf-8");
y= new String(symptom.getBytes("ISO8859_1"),"utf-8");//解决jsp中文乱码问题
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:abc";
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
PreparedStatement stmt=null; //注意:是PreparedStatement,而不是Statement!
String sql="insert into sys.abc(x,y) values (?,?)";
stmt=conn.prepareStatement(sql);
stmt.setString(1,x);//索引从1开始
stmt.setString(2,y);
stmt.executeUpdate(); //注意没有sql!!否则报ORA-01008: 并非所有变量都已绑定
stmt.close();
conn.close();
/* */
}
catch(Exception e){e.printStackTrace();}
%>
二、Android端
package com.test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
//import com.test.AndroidLinkToJsp.R;
import ContentHandler.ContentHandler;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OracleActivity extends Activity {
private TextView myText;
private Button myButton;
//命名空间
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//为了解决网络异常
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
myText = (TextView)findViewById(R.id.myText);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new postdata());
}
class postdata implements OnClickListener{
//获得EditText控件的x、y值
public void onClick(View v) {
EditText Editx=(EditText)findViewById(R.id.editTextx);
String x=myEdit.getText().toString();
EditText Edity=(EditText)findViewById(R.id.editTexty);
String y=myEdit.getText().toString();
//访问JSP
String urlPath="http://192.168.1.118:8080/Android_Server/post.jsp"; //注意不要用localhost/127.0.1.1
/*建立HTTP Post连线*/
HttpPost httpRequest =new HttpPost(urlPath);
//Post运作传送变数必须用NameValuePair[]阵列储存
//传参数 服务端获取的方法为request.getParameter("name")
List <NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("x",mysymptom));
params.add(new BasicNameValuePair("y",mysymptom));
try{
//发出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若状态码为200 ok
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出回应字串
System.out.println("OK");
}
else
{
// textView1.setText("Error Response"+httpResponse.getStatusLine().toString());
}
}
catch(ClientProtocolException e){
//textView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// textView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
// textView1.setText(e.getMessage().toString());
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}
}
}
浙公网安备 33010602011771号