文章來源:http://xevan.net/bae-for-android/
在做android應用的時候,我們不可避免的要存取資料,而且更多的時候用的就是本地的MySQL伺服器,但是通過百度的BAE卻能實現資料的遠程存取,即在公網內訪問。本文就簡單的通過一個執行個體來向大家說明怎樣使用BAE來使用雲資料庫。
1、什麼是BAE?
百度雲環境,即BAE(百度應用引擎),提供多語言、彈性的服務端運行環境,能協助開發人員快速開發並部署應用。雲環境內建豐富的分散式運算API,並支援全方位的百度“雲”服務,更能為您的應用帶來強大動力,從“本地”變“分布式”,簡單可依賴。具體可參見上面的官網連結。
我們這裡用到的就是百度的雲端儲存。
2、部署雲環境
註冊百度帳號,點擊管理中心,填寫相關的個人資訊。並啟用郵箱。
點擊建立應用,選擇web應用,填寫應用程式名稱,並選擇PC Iframe。
選擇雲環境,進行手機驗證,然後進行託管設定,網域名稱隨意選取,並選擇環境類型為java,確定,提示建立應用成功。
回到基本資料處,此時應用已經有了AppID(BAE託管)。
點擊雲環境---->雲資料庫---->建立資料庫,當然是用免費的了。
此時雲環境部署成功,
3、向雲端資料庫寫入測試資料。
點擊操作欄下的phpmyadmin(這是用php寫成的用於管理mysql資料庫的可視化應用程式)(註:safari瀏覽器無法開啟,如果你不能開啟的時候嘗試換用其它瀏覽器)
開啟介面如下
建立一張表,名字隨意填寫,這裡為evanTest,欄位數是指有幾個變數,這裡填寫3個吧。並點擊執行,填寫變數名字和類型和長度,點擊儲存
點擊資料表後邊的插入選項,我們寫入一組資料吧,並點擊執行
至此,資料表裡就多了一組資料。
4、搭建本地WEB應用並部署至BAE雲端
點擊這裡安裝外掛程式,或者直接點擊這裡(一鍵安裝)下載整合的開發環境。
首先我們在安裝好外掛程式或者下載好的eclipse上能看到百度的標誌,點擊並選擇New BAE Project(Java)
我的項目取名為evan,建立後我們發現跟一般的web伺服器基本是一致的,而且還給我們省去了很多搭建各種環境的步驟,很是方便!
我們直接修改HelloWorldServlet.java的代碼(這個類其實就是Servlet),在這裡邊處理android用戶端的請求。
檔案代碼如下:
package test;import java.io.DataOutputStream;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.baidu.bae.api.util.BaeEnv;public class HelloWorldServlet extends HttpServlet {/** * */private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubString host = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_IP);String port = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_PORT);String username = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_AK);String password = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_SK);String driverName = "com.mysql.jdbc.Driver";String dbUrl = "jdbc:mysql://";String serverName = host + ":" + port + "/";// 從平台查詢應用要使用的資料庫名String databaseName = "HDxhGItkNqwdecJzXubX";String connName = dbUrl + serverName + databaseName;String sql = "select * from evanTest";String info;Connection connection = null;Statement stmt = null;ResultSet rs = null;try {Class.forName(driverName);// 具體的資料庫操作邏輯connection = DriverManager.getConnection(connName, username,password);stmt = connection.createStatement();rs = stmt.executeQuery(sql);String m_name = "", m_school = "", m_city = "";DataOutputStream output = new DataOutputStream(resp.getOutputStream() );while (rs.next()) {m_name = rs.getString("name");m_school = rs.getString("school");m_city = rs.getString("city");resp.setContentType("text/plain");info = m_name + "_" + m_school + "_" + m_city;output.writeUTF(info);}output.close();} catch (ClassNotFoundException ex) {// 異常處理邏輯} catch (SQLException e) {// 異常處理邏輯} finally {try {if (connection != null) {connection.close();}} catch (SQLException e) {}}}}
將第36行改為自己資料庫的名字,資料庫的名字在建立資料庫的時候已經給了。
將第38行from後面改為自己表的名字。
點擊百度標誌---->Deploy To BAE,點擊Project Properties
填寫下面的表,ApplicationID就是之前我們進行手機驗證之後的AppID(BAE託管),Access Key(就是API Key)和Secure Key在百度我們的應用頁面也很容易找到
填寫完畢後點OK,再點擊Deploy,輸入自己的帳號密碼,部署成功後會顯示,隨便選擇就可以。
在瀏覽器輸入我們最開始填入的網域名稱,我填的是zyfevan.duapp.com,在瀏覽器輸入1.zyfevan.duapp.com,會顯示
說明成功了,這個頁面就是我們剛才的項目裡的index.jsp顯示的,若輸入1.zyfevan.duapp.com/hello同樣會得到資料庫裡邊的資料,Servlet的定義是在項目WEB-INF的web.xml裡定義的
至此伺服器搭建完畢。
5、在android用戶端擷取伺服器的資料
建立一個android項目,首先我們布局一下介面,activity_main.xml的代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/m_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/m_textView" android:layout_centerHorizontal="true" android:layout_marginTop="22dp" android:text="從資料庫擷取東西" /> <TextView android:id="@+id/m_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="這是原始的東西" /></RelativeLayout>
然後再改寫主檔案,MainActivity.java的代碼如下:
package com.example.testtaxi;import java.io.ByteArrayInputStream;import java.io.DataInputStream;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.util.StringTokenizer;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;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;import android.os.Bundle;import android.os.Handler;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private Button m_button;private TextView m_textView;private String url = "http://1.zyfevan.duapp.com/hello";private StringTokenizer info;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);m_button = (Button) findViewById(R.id.m_button);m_textView = (TextView) findViewById(R.id.m_textView);m_button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(new Runnable() {public void run() {try {HttpGet httpRequest = new HttpGet(url);HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// 判斷請求是否成功if (httpResponse.getStatusLine().getStatusCode() == 200) {HttpEntity entity = httpResponse.getEntity();if (entity != null) {info = new StringTokenizer(EntityUtils.toString(entity, "utf-8"), "_");if (info.countTokens() % 3 != 0) {Log.v("error", "不夠3段");}m_textView.post(new Runnable() {public void run() {m_textView.setText("name:"+ info.nextToken()+ " school:"+ info.nextToken()+ " city:"+ info.nextToken());}});}}} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}).start();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
其中第33行為我們Servlet的連結
49---60行為通過網路擷取資料的代碼
需要特別說明的兩點是:
- 從android4.0開始就不允許在主線程裡訪問網路,所以在第46行我們線上程中訪問網路
- 對View的操作不能再線程中進行,所以第61行我們使用了官網介紹的post方法,用Runnable來修改TextView的內容
運行效果,剛開啟程式:
點擊按鈕後:
說明我們能從雲端資料庫擷取資料了。