使用 ksoap2
http://code.google.com/p/ksoap2-android/
下載後將.jar 將檔案丟到專案資料夾內
對著專案按滑鼠右鍵,選 Build Path / Configure Build Path
選 Add JARs,然後選擇剛剛丟進資料夾的 jar 檔後按 ok
此時左方 Package Explorer 就會出現 Referenced Libraries 資料夾,表示引用成功
再來是視圖 layout
首先先將Activity的背景圖放置以下三個資料夾
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="bottom"
android:layout_marginBottom="65dip" android:id="@+id/layout2">
<TableLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_marginLeft="35dip"
android:layout_marginRight="35dip">
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="你的帳號" android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
/>
<EditText android:layout_height="wrap_content"
android:layout_weight="5" android:layout_width="match_parent"
android:id="@+id/account"
/>
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="你的密碼" android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content" />
<EditText android:layout_height="wrap_content" android:password="true"
android:layout_width="match_parent" android:layout_weight="5"
android:id="@+id/password"
/>
</TableRow>
<TableRow android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="92dip">
<Button android:id="@+id/submit" android:text="會員登入" />
<Button android:id="@+id/join" android:text="加入會員" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
程式
package com.hello;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
import android.widget.Toast;
publicclass hello extends Activity {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
layout.setBackgroundResource(R.drawable.bgimg);
Button button = (Button)findViewById(R.id.submit);
button.setOnClickListener(Login);
}
private OnClickListener Login = new OnClickListener (){
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
String NAMESPACE = "http://www.xxxxxx.com/" ;
String URL = "http://192.168.111.217/Service.asmx?WS";//?WSDL";
String MemberLogin_SOAP_ACTION = "http://www.brand333.com/Get_LoginStatus";
String METHOD_NAME = "Get_LoginStatus";
EditText Account = (EditText)findViewById(R.id.account);
EditText Password = (EditText)findViewById(R.id.password);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Account", Account.getText().toString());
request.addProperty("Password", Password.getText().toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try
{
androidHttpTransport.call(MemberLogin_SOAP_ACTION, envelope);
//若是單純的值,直接用SoapPrimitive接
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
boolean success = Boolean.parseBoolean(result.toString());
if (success)
{
Toast.makeText(hello.this, "登入成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(hello.this, "登入失敗", Toast.LENGTH_LONG).show();
}
}
catch (Exception e)
{
Toast.makeText(hello.this, e.toString(), Toast.LENGTH_LONG).show();
}
}
};
}
參考資料 :
http://www.dotblogs.com.tw/alonstar/archive/2011/05/27/26345.aspx
http://code.google.com/p/ksoap2-android/wiki/HowToUse