WebService的简单使用

来源:互联网 发布:js获取所有兄弟元素 编辑:程序博客网 时间:2024/06/13 10:26
示例代码,通过webservice查询电话归属地设置网络权限一个EditText  一个Button  还有一个TextView  通过button的点击事件  将输入的电话号码的归属地   设置在textview上public class MainActivity extends AppCompatActivity {    private TextView text;    private EditText edit;    private Button button;    private String edtext;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        intiView();    }    //初始化数据    private void intiView() {        text = (TextView) findViewById(R.id.text_1);        edit = (EditText) findViewById(R.id.edit_1);        button = (Button) findViewById(R.id.button);    }    //点击事件  查询  耗时操作,放在子线程    public void qurry(View view){        new Thread(){            @Override            public void run() {                super.run();                edtext = edit.getText().toString();                final String s;                try {                    s = qurryPhone(edtext);                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                            text.setText(s);                        }                    });                } catch (Exception e) {                    e.printStackTrace();                }            }        }.start();    }    //查询电话归属地的方法    public String qurryPhone(String phone){        try {            String servers_Uri="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";            String name_space="http://WebXml.com.cn/";            String method_name="getMobileCodeInfo";            //创建信封            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);            //创建信件            SoapObject soapObject = new SoapObject(name_space, method_name);            soapObject.addProperty("mobileCode",phone);//        soapObject.addProperty("theUserID","3cdded86be454f258aed2a0610295961");            //将信件装进信封            envelope.bodyOut=soapObject;            //设置跨平台的兼容性            envelope.dotNet=true;            //创建连接            HttpTransportSE httpTransportSE = new HttpTransportSE(servers_Uri);            //发送消息            httpTransportSE.call(name_space + method_name,envelope);            //服务器响应  如果消息不为空  证明服务器响应了            if(envelope.getResponse() != null){                SoapObject result = (SoapObject) envelope.bodyIn;                String local=result.getProperty("getMobileCodeInfoResult").toString();                return local;            }        } catch (IOException e) {            e.printStackTrace();        } catch (XmlPullParserException e) {            e.printStackTrace();        }        return null;    }} 

0 0
原创粉丝点击