<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@string/hello" />
<Button android:text="Button" android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_width="fill_parent" android:id="@+id/mobile" android:text="mobile" android:layout_height="wrap_content"></EditText>
<TextView android:text="TextView" android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
package com.example;import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.util.Log;
import android.util.Xml;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
public class MobileService {
public static String getMobileAddress(String mobile) throws Exception {
String path = "http://192.168.0.38/pdaService/Service.asmx?wsdl"; InputStream is = MobileService.class.getClassLoader().getResourceAsStream("mobile.xml");
byte[] data = StreamTool.readInputStream(is);
String xmlStr = new String(data); /* 替换掉 手机号码的占位符 */
xmlStr = xmlStr.replaceAll("\\$mobile", mobile);
Log.i("MobileService", xmlStr); data = xmlStr.getBytes("UTF-8");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求参数与请求头
conn.setReadTimeout(4*1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", "" + data.length); // 将数据写往服务器
OutputStream os = conn.getOutputStream(); 在这里报错~!
os.write(data);
os.flush();
os.close(); if(conn.getDoInput()) {}
/* 解析服务器返回的信息 */
is = conn.getInputStream();
return parseInput(is); }
/**
* 从输入流中解析出号码归属地
*/
private static String parseInput(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "UTF-8");
int et = parser.getEventType();
while(et != XmlPullParser.END_DOCUMENT) {
switch(et) {
case XmlPullParser.START_TAG :
String tagName = parser.getName();
if(tagName != null && "getMobileCodeInfoResult".equals(tagName)) {
return parser.nextText();
}
}
et = parser.next();
}
return "";
}
}package com.example;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
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 org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;public class MainActivity extends Activity {
protected static final String TAG = null;
private TextView resultView;
private EditText mobileText;
Handler handler=new Handler();
// protected static final EditText mobileText =null;
// protected static final Button button =null;
// protected static final TextView resultView =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /* 用于输入待查手机号码文本框 */
mobileText = (EditText) findViewById(R.id.mobile);
/* 用于输出查询结果的标签 */
resultView = (TextView) findViewById(R.id.result);
/* 确认查询按钮 */
Button button = (Button) findViewById(R.id.button);
/* 为其绑定点击事件 */
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String mobile = mobileText.getText().toString();
try {
/* 调用业务方法,获取手机归属地 */
String result = MobileService.getMobileAddress(mobile);
resultView.setText(result);
} catch (Exception e) {
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString());
}
}
}); }
}package com.example;import java.io.ByteArrayOutputStream;
import java.io.InputStream;public class StreamTool {
public static byte[] readInputStream(InputStream is) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len; while((len = is.read(buffer)) != -1 ) {
bos.write(buffer, 0, len);
}
is.close();
bos.close();
return bos.toByteArray();
} }
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>$mobile</mobileCode> <!-- $mobile 为值定义的一个占位符,在程序中读取此 XMl 之后,需要将它替换成用户输入的手机号码 -->
<userID></userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope><?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>返回的手机号码归属地</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap12:Body></soap12:Envelope>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@string/hello" />
<Button android:text="Button" android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<EditText android:layout_width="fill_parent" android:id="@+id/mobile" android:text="mobile" android:layout_height="wrap_content"></EditText>
<TextView android:text="TextView" android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
package com.example;import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.xmlpull.v1.XmlPullParser;
import android.util.Log;
import android.util.Xml;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
public class MobileService {
public static String getMobileAddress(String mobile) throws Exception {
String path = "http://192.168.0.38/pdaService/Service.asmx?wsdl"; InputStream is = MobileService.class.getClassLoader().getResourceAsStream("mobile.xml");
byte[] data = StreamTool.readInputStream(is);
String xmlStr = new String(data); /* 替换掉 手机号码的占位符 */
xmlStr = xmlStr.replaceAll("\\$mobile", mobile);
Log.i("MobileService", xmlStr); data = xmlStr.getBytes("UTF-8");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置请求参数与请求头
conn.setReadTimeout(4*1000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("Content-Length", "" + data.length); // 将数据写往服务器
OutputStream os = conn.getOutputStream(); 在这里报错~!
os.write(data);
os.flush();
os.close(); if(conn.getDoInput()) {}
/* 解析服务器返回的信息 */
is = conn.getInputStream();
return parseInput(is); }
/**
* 从输入流中解析出号码归属地
*/
private static String parseInput(InputStream is) throws Exception {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(is, "UTF-8");
int et = parser.getEventType();
while(et != XmlPullParser.END_DOCUMENT) {
switch(et) {
case XmlPullParser.START_TAG :
String tagName = parser.getName();
if(tagName != null && "getMobileCodeInfoResult".equals(tagName)) {
return parser.nextText();
}
}
et = parser.next();
}
return "";
}
}package com.example;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
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 org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;public class MainActivity extends Activity {
protected static final String TAG = null;
private TextView resultView;
private EditText mobileText;
Handler handler=new Handler();
// protected static final EditText mobileText =null;
// protected static final Button button =null;
// protected static final TextView resultView =null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); /* 用于输入待查手机号码文本框 */
mobileText = (EditText) findViewById(R.id.mobile);
/* 用于输出查询结果的标签 */
resultView = (TextView) findViewById(R.id.result);
/* 确认查询按钮 */
Button button = (Button) findViewById(R.id.button);
/* 为其绑定点击事件 */
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String mobile = mobileText.getText().toString();
try {
/* 调用业务方法,获取手机归属地 */
String result = MobileService.getMobileAddress(mobile);
resultView.setText(result);
} catch (Exception e) {
Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString());
}
}
}); }
}package com.example;import java.io.ByteArrayOutputStream;
import java.io.InputStream;public class StreamTool {
public static byte[] readInputStream(InputStream is) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len; while((len = is.read(buffer)) != -1 ) {
bos.write(buffer, 0, len);
}
is.close();
bos.close();
return bos.toByteArray();
} }
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>$mobile</mobileCode> <!-- $mobile 为值定义的一个占位符,在程序中读取此 XMl 之后,需要将它替换成用户输入的手机号码 -->
<userID></userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope><?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>返回的手机号码归属地</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap12:Body></soap12:Envelope>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货