1.准备工作
(1)下载ksoap2-android-assembly-xxx-jar-with-dependencies.jar包
下载地址:
下载自己需要的版本号就行,我用的是
ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar
(2)导入jar包
首先,找到下载好的jar包并复制到安卓的libs目录下
然后Add as libaray
这样就完成jar包的导入啦!
如果找不到libs目录,可以先切换为package,再执行第一张图的操作
我用的模拟器的api为26
2.接下来上代码了
(1)首先在AndroidManifest.xml中添加网络权限,前提是之前已经配置过sdk路径了,如何开启的网络权限,第一次环境变量配置以后要重启喔,这样才会生效。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
android:usesCleartextTraffic="true"
</application>
加完以后,可以先在模拟器中的浏览器随便浏览一个网站,看是否联网了
(2)MainActivity.java的代码如下:
package com.example.weather;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends AppCompatActivity {
private EditText phoneSecEditText;
private TextView resultView;
private Button queryButton;
private String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneSecEditText = (EditText) findViewById(R.id.phone_sec);
resultView = (TextView) findViewById(R.id.result_text);
queryButton = (Button) findViewById(R.id.query_btn);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 手机号码(段)
String phoneSec = phoneSecEditText.getText().toString().trim();
// 简单判断用户输入的手机号码(段)是否合法
if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 给出错误提示
phoneSecEditText.setError("您输入的手机号码(段)有误!");
phoneSecEditText.requestFocus();
// 将显示查询结果的TextView清空
resultView.setText("");
return;
}
//启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
QueryAddressTask queryAddressTask = new QueryAddressTask();
//启动后台任务
Log.d("你", phoneSec);
queryAddressTask.execute(phoneSec);
}
});
}
/**
* 手机号段归属地查询
*
* @param phoneSec 手机号段
*/
public String getRemoteInfo(String phoneSec) throws Exception{
String WSDL_URI = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getDatabaseInfo";//wsdl 的uri
String namespace = "http://WebXml.com.cn/";//namespace
String methodName = "getMobileCodeInfo";//要调用的方法名称
SoapObject request = new SoapObject(namespace, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
request.addProperty("mobileCode", phoneSec);
request.addProperty("userId", "");
//创建SoapSerializationEnvelope 对象,同时指定soap版本号(之前在wsdl中看到的)
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
envelope.bodyOut = request;//由于是发送请求,所以是设置bodyOut
envelope.dotNet = true;//由于是.net开发的webservice,所以这里要设置为true
HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
httpTransportSE.call(null, envelope);//调用
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
result = object.getProperty(0).toString();
Log.d("debug",result);
return result;
}
class QueryAddressTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// 查询手机号码(段)信息*/
try {
result = getRemoteInfo(params[0]);
} catch (Exception e) {
e.printStackTrace();
}
//将结果返回给onPostExecute方法
return result;
}
@Override
//此方法可以在主线程改变UI
protected void onPostExecute(String result) {
// 将WebService返回的结果显示在TextView中
Toast.makeText(MainActivity.this, "对了", Toast.LENGTH_SHORT).show();
resultView.setText(result);
}
}
}
(3)activity_main.xml的代码如下:
<LinearLayout 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:orientation="vertical"
android:padding="5dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/phone_sec"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入查询的相关参数" />
<Button
android:id="@+id/query_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="号码归属地查询" />
<TextView
android:id="@+id/result_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="结果显示:"
android:textSize="16sp" />
</LinearLayout>
结语:这是我第一次写csdn,如有错误,欢迎指正批评。如果该代码无法运行时,可以尝试换个api。大家做这个项目的时候,可以尝试在以下代码中的双引号位置,用ctrl+B看是否能自动跳转到浏览器中的界面
String WSDL_URI = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getDatabaseInfo";
好了,写完了!!!