android----发送soap数据给服务器调用webservice,实现手机归属地查询

 

public class MobileService {
 /**
  * 获取手机号归属地
  * @param mobile
  * @return
  * @throws Exception
  */
 public static String getAddress(String mobile) throws Exception{
  String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
  InputStream insInputStream = MobileService.class.getClassLoader().getResourceAsStream("soap.xml");
  String soap = readFile(insInputStream);
  soap = soap.replaceAll("\\$mobile", mobile);
  byte[]data = soap.getBytes();
  HttpURLConnection connection  = (HttpURLConnection) new URL(path).openConnection();
  connection.setConnectTimeout(5000);
  connection.setRequestMethod("POST");
  connection.setDoOutput(true);
  connection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
  connection.setRequestProperty("Content-Length", String.valueOf(data.length));
  connection.getOutputStream().write(data);
  if(connection.getResponseCode()==200){
   return parseXML(connection.getInputStream());
  }
  return null;
 }
 /**
  * 解析返回的SOAP协议
  * @param inputStream
  * @return
  */
 private static String parseXML(InputStream inputStream)throws Exception {
  XmlPullParser parser = Xml.newPullParser();
  parser.setInput(inputStream,"UTF-8");
  int event = parser.getEventType();
  while(event!=XmlPullParser.END_DOCUMENT){
   switch (event) {
   case XmlPullParser.START_TAG:
    if("getMobileCodeInfoResult".equals(parser.getName())){
     return parser.nextText();
    }
    break;
   }
   event = parser.next();
  }
  return null;
 }

 private static String readFile(InputStream insInputStream) throws Exception {
  byte[] data = StreamTool.read(insInputStream);
  return new String(data);
 }
}

public class StreamTool {
 /**
  * 读取输入流数据
  * @param inStream
  * @return
  */
 public static byte[] read(InputStream inStream) throws Exception{
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while( (len = inStream.read(buffer)) != -1 ){
   outStream.write(buffer, 0, len);
  }
  inStream.close();
  return outStream.toByteArray();
 }

}


<?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:text="@string/inputnum"
    />
    <EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/number"
    />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/find"
    android:id="@+id/button"
    />
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/address"
    />
</LinearLayout>

public class MainActivity extends Activity {
    private EditText mobileText;
    private TextView addressView;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        mobileText = (EditText) this.findViewById(R.id.number);
        addressView = (TextView) this.findViewById(R.id.address);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new ButtonClickListener());
    }
   
    private final class ButtonClickListener implements View.OnClickListener{

  public void onClick(View v) {
   String mobile = mobileText.getText().toString();
   try {
    String address = MobileService.getAddress(mobile);
    addressView.setText(address);
    Toast.makeText(getApplicationContext(), R.string.success, 1).show();
   } catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), R.string.failed, 1).show();
   }
  }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值