編輯:關於 Android 程式設計
以前做專案只要用到資料庫,都在 App 裡自建一份資料,但這麼做並不可取:手機記憶體有限,把資料表建在專案裡無疑又增大了程式體積,運行速度可想而知;而且資料量大時放在資料庫更合適,不僅方便還能達到同步效果。
很多應用所依賴的資料都存在資料庫裡,這時既精簡又能同步到資料庫的方法,就是透過 WebService(SOAP 協議)連線取得資料庫資訊。以下做個小範例。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<Button android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class SOAPUtil {
public static Object doTransport(final String wsdUrl, final String webMethod) {
String nameSpace = "https://tempuri.org/"; // 一般都是預設的
SoapObject soapObject = new SoapObject(nameSpace, webMethod);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyIn = soapObject;
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(wsdUrl);
String SOAP_ACTION = "https://tempuri.org/" + webMethod;
try {
httpTransportSE.call(SOAP_ACTION, envelope);
if (envelope.getResponse() != null) {
SoapObject result = (SoapObject) envelope.getResponse();
for (int i = 0; i < result.getPropertyCount(); i++) {
System.out.println("result [" + i + "] = " + result.getProperty(i).toString());
}
return result;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
}
public class MainActivity extends Activity {
private Button searchs;
private TextView results;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchs = (Button) findViewById(R.id.search);
results = (TextView) findViewById(R.id.result);
searchs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String wsdUrl = "https://192.168.1.195:88/service1.asmx"; // 伺服器位址
String method = "SelectAll"; // 方法名
Object result = SOAPUtil.doTransport(wsdUrl, method);
results.setText(result.toString());
}
});
}
}
以上是客戶端必要步驟,此外還需伺服器端提供 WebService 介面(介面名即 Activity 中呼叫的方法名)。介面本身並不複雜,各語言都能實作,核心就是 SQL 操作語句,寫好部署到伺服器即可。此範例使用 ksoap2 相關類別,運作前需在專案中加入對應的第三方函式庫,並記得加入網路權限。
Android 模擬登入並評論 CSDN 的實作解析
有些應用會需要模擬網站登入與互動,例如自動發佈評論。本文以 CSDN 為例,說明如何分析表單欄位、取得動態參數,並以 HTTP 請求完成登入與留言。實際操作時仍需遵守網站
OkHttp 基礎用法與二次封裝詳解:Get、Post 與 Gson 集成
日常開發中,網絡層往往由同事封裝好,我們只管調用。但一旦要自己動手封裝,就會發現細節不少:線程切換、參數組織、返回數據的泛型解析、統一異常處理。本文從最原始的 get /
尋找 Android 手機垃圾文件的根源:揪出無名文件夾
Android 系統中的很多安全類軟體都支援垃圾清理,但總有一些不知名文件夾能逃過這類功能的「洗禮」。那麼,我們如何根據蛛絲馬跡了解這些文件夾的身份,並在斷定無用後安心刪
Android SlidingDrawer 滑動式抽屜詳解:屬性、方法與完整範例
SlidingDrawer 的效果其實大家都看過——早期 Android 1.5 模擬器進入應用程式列表時,從畫面下方拉出抽屜的那個互動,就是它。 一、基本概念 Sl