返回顶部

收藏

android代码查询手机归属地

更多

android访问webservice获取手机归属地

public class MobileService {
    public static String getMobilAdress(String mobile) throws Exception{
        InputStream inputStream = MobileService.class.getClassLoader().getResourceAsStream("mobilesaop.xml");
        byte[] data = ReadInputStram(inputStream);
        String xml=new String(data);
        String soap= xml.replaceAll("\\$mobile",mobile);
        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        data = soap.getBytes();
        HttpURLConnection conn =(HttpURLConnection)new URL(path).openConnection();
        conn.setReadTimeout(5000);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
        conn.setRequestProperty("Content-Length", String.valueOf(data.length));
        OutputStream out = conn.getOutputStream();
        out.write(data);
        out.flush();
        out.close();
        if(conn.getResponseCode() ==200){
            return parseXML(conn.getInputStream());
        }
        return null;
    }
    /**
     * 解析返回的xml数据
     * @param inputStream
     * @return
     * @throws Exception
     */
    public  static String parseXML(InputStream inputStream) throws Exception {
        XmlPullParser pullparse =Xml.newPullParser();
        pullparse.setInput(inputStream,"UTF-8");
        int event = pullparse.getEventType();
        while(event!=XmlPullParser.END_DOCUMENT){
            switch (event) {
            case XmlPullParser.START_TAG:
                if("getMobileCodeInfoResult".equals(pullparse.getName())){
                    return pullparse.nextText();
                }
                break;
            }
            event = pullparse.next();
        }
        return null;
    }

    public static byte[] ReadInputStram(InputStream inputStream) throws Exception{
        ByteArrayOutputStream outStream =new ByteArrayOutputStream();
        byte[] buf =new byte[1024];
        int len ;
        while((len=inputStream.read(buf))!=-1){
            outStream.write(buf, 0, len);
        }
        return outStream.toByteArray();
    }

[Java]代码

public class PhoneActivity extends Activity {
    private static String TAG = "PhoneActivity";
    private EditText phonenumberText;
    private TextView textView;
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        phonenumberText = (EditText)this.findViewById(R.id.phonenumber);
        textView = (TextView)this.findViewById(R.id.textView);
        button =(Button)this.findViewById(R.id.query);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String phone =phonenumberText.getText().toString();
                try {
                    String address =MobileService.getMobilAdress(phone);
                    textView.setText(address);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, e.toString());
                    Toast.makeText(getApplicationContext(), R.string.error, 1).show();
                }

            }});
    }

[Java]代码

<?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>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>$mobile</mobileCode>
      <userID></userID>
    </getMobileCodeInfo>
  </soap12:Body>
</soap12:Envelope>

标签:java,android

收藏

0人收藏

支持

0

反对

0

发表评论