关于Android连接远程数据库(mysql、oracle)

关于Android连接远程数据库(mysql、oracle)

2014年07月17日 15:43:33

阅读数:1969

前提:假设远程有一个oracle的数据库,并且有一个已经连接(JDBC操作)了该DB的server(AndroidServer),IP为:http://192.168.0.12:8080/AndroidServer/。
测试程序功能:实现登录功能。
1,当然是先建一个Android 的 project啦关于Android连接远程数据库(mysql、oracle) - Jansun - 茅庐
2,建一个HTTP的工具类HttpUtil来获得HttpRequest对象和HttpResponse对象,以及发送get和post请求获得返回response信息的方法:

package android.ocacleconnect.util;

 

import java.io.IOException;

 

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

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.util.EntityUtils;

 

public class HttpUtil {

// 基础URL

public static final String BASE_URL="http://192.168.0.12:8080/AndroidServer/";

// 获得Get请求对象request

public static HttpGet getHttpGet(String url){

HttpGet request = new HttpGet(url);

return request;

}

// 获得Post请求对象request

public static HttpPost getHttpPost(String url){

HttpPost request = new HttpPost(url);

return request;

}

// 根据请求获得响应对象response

public static HttpResponse getHttpResponse(HttpGet request) throws ClientProtocolException, IOException{

HttpResponse response = new DefaultHttpClient().execute(request);

return response;

}

// 根据请求获得响应对象response

public static HttpResponse getHttpResponse(HttpPost request) throws ClientProtocolException, IOException{

HttpResponse response = new DefaultHttpClient().execute(request);

return response;

}

// 发送Post请求,获得响应查询结果

public static String queryStringForPost(String url){

// 根据url获得HttpPost对象

HttpPost request = HttpUtil.getHttpPost(url);

String result = null;

try {

// 获得响应对象

HttpResponse response = HttpUtil.getHttpResponse(request);

// 判断是否请求成功

if(response.getStatusLine().getStatusCode()==200){

// 获得响应

result = EntityUtils.toString(response.getEntity());

return result;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

result = "网络异常!";

return result;

} catch (IOException e) {

e.printStackTrace();

result = "网络异常!";

return result;

}

        return null;

    }

// 获得响应查询结果

public static String queryStringForPost(HttpPost request){

String result = null;

try {

// 获得响应对象

HttpResponse response = HttpUtil.getHttpResponse(request);

// 判断是否请求成功

if(response.getStatusLine().getStatusCode()==200){

// 获得响应

result = EntityUtils.toString(response.getEntity());

return result;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

result = "网络异常!";

return result;

} catch (IOException e) {

e.printStackTrace();

result = "网络异常!";

return result;

}

        return null;

    }

// 发送Get请求,获得响应查询结果

public static  String queryStringForGet(String url){

// 获得HttpGet对象

HttpGet request = HttpUtil.getHttpGet(url);

String result = null;

try {

// 获得响应对象

HttpResponse response = HttpUtil.getHttpResponse(request);

// 判断是否请求成功

if(response.getStatusLine().getStatusCode()==200){

// 获得响应

result = EntityUtils.toString(response.getEntity());

return result;

}

} catch (ClientProtocolException e) {

e.printStackTrace();

result = "网络异常!";

return result;

} catch (IOException e) {

e.printStackTrace();

result = "网络异常!";

return result;

}

        return null;

    }

}


3. 建一个LoginActivity的activity,调用刚写的HttpUtil工具来实现连接远程的数据库

package android.oracleconnect.activity;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

import android.oracleconnect.HttpUtil;

 

public class LoginActivity extends Activity {

// 声明登录、取消按钮

private Button cancelBtn,loginBtn;

// 声明用户名、密码输入框

private EditText userEditText,pwdEditText;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// 设置标题

setTitle("Android远程数据库连接测试-用户登录");

// 设置当前Activity界面布局

setContentView(R.layout.login_system);

// 通过findViewById方法实例化组件

cancelBtn = (Button)findViewById(R.id.cancelButton);

// 通过findViewById方法实例化组件

loginBtn = (Button)findViewById(R.id.loginButton);

// 通过findViewById方法实例化组件

userEditText = (EditText)findViewById(R.id.userEditText);

// 通过findViewById方法实例化组件

pwdEditText = (EditText)findViewById(R.id.pwdEditText);

cancelBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

finish();

}

});

loginBtn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if(validate()){

if(login()){

Intent intent = new Intent(LoginActivity.this,MainMenuActivity.class);

startActivity(intent);

}else{

showDialog("用户名称或者密码错误,请重新输入!");

}

}

}

});

}

// 登录方法

private boolean login(){

// 获得用户名称

String username = userEditText.getText().toString();

// 获得密码

String pwd = pwdEditText.getText().toString();

// 获得登录结果

String result=query(username,pwd);

System.out.println("result:"+result);

if(result!=null&&result.equals("0")){

return false;

}else{

saveUserMsg(result);

return true;

}

}

// 将用户信息保存到配置文件

private void saveUserMsg(String msg){

// 用户编号

String id = "";

// 用户名称

String name = "";

// 获得信息数组

String[] msgs = msg.split(";");

int idx = msgs[0].indexOf("=");

id = msgs[0].substring(idx+1);

idx = msgs[1].indexOf("=");

name = msgs[1].substring(idx+1);

// 共享信息

SharedPreferences pre = getSharedPreferences("user_msg", MODE_WORLD_WRITEABLE);

SharedPreferences.Editor editor = pre.edit();

editor.putString("id", id);

editor.putString("name", name);

editor.commit();

}

// 验证方法

private boolean validate(){

String username = userEditText.getText().toString();

if(username.equals("")){

showDialog("用户名称是必填项!");

return false;

}

String pwd = pwdEditText.getText().toString();

if(pwd.equals("")){

showDialog("用户密码是必填项!");

return false;

}

return true;

}

private void showDialog(String msg){

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage(msg)

      .setCancelable(false)

      .setPositiveButton("确定", new DialogInterface.OnClickListener() {

          public void onClick(DialogInterface dialog, int id) {

          }

      });

AlertDialog alert = builder.create();

alert.show();

}

// 根据用户名称密码查询

private String query(String account,String password){

// 查询参数

String queryString = "account="+account+"&password="+password;

// url

String url = HttpUtil.BASE_URL+"servlet/LoginServlet?"+queryString;

// 查询返回结果

return HttpUtil.queryStringForPost(url);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值