先看一下运行图:
下面看一下具体代码:
package com.phonesearch;
import com.phonesearch.web.SearchActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText phoneText;
private Button search;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找组件
phoneText=(EditText) this.findViewById(R.id.editText1);
search=(Button) this.findViewById(R.id.button1);
resultView=(TextView) this.findViewById(R.id.result);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone=phoneText.getText().toString();
String wsdlUrl="http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
String method="getMobileCodeInfo";
Object result=SearchActivity.doTrantsport(wsdlUrl, method,phone);
resultView.setText(result.toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
SearchActivity.java
package com.phonesearch.web;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
public class SearchActivity{
public static Object doTrantsport(final String wsdlUrl,final String webMethod,String mobile){
String nameSpace = "http://WebXml.com.cn/";
SoapObject soapObject=new SoapObject(nameSpace, webMethod);
soapObject.addProperty("mobileCode",mobile);
soapObject.addProperty("userID",null);
SoapSerializationEnvelope soapSerializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapSerializationEnvelope.bodyOut=soapObject;
soapSerializationEnvelope.dotNet=true;
soapSerializationEnvelope.setOutputSoapObject(soapObject);
System.out.println(wsdlUrl);
HttpTransportSE httpTransportSE = new HttpTransportSE(wsdlUrl);
String SOAP_ACTION="http://WebXml.com.cn/"+webMethod;
System.out.println("*****"+SOAP_ACTION);
try {
httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
if(soapSerializationEnvelope.getResponse() != null){
Object result = soapSerializationEnvelope.getResponse();
System.out.println("soapSerializationEnvelope.getResponse()="+result);
return result;
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (XmlPullParserException e1) {
e1.printStackTrace();
}
return null;
}
}
布局文件:
<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:background="@drawable/blue" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="phone"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="@string/search" />
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1" />
</RelativeLayout>
strings.java
<resources>
<string name="app_name">PhoneSearch</string>
<string name="hello_world">Please input your telephone number:</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="search">Search</string>
</resources>
接下来,我们需要设置一下权限:
<!-- 允许访问网络 -->
<uses-permission android:name="android.permission.INTERNET"/>
大功告成了!!!