20150618_Andriod _KSOAP2_多线程

 

参考地址:http://blog.csdn.net/long704480904/article/details/8636734

webService:基于SOAP协议的远程调用标准,通过webService可以将不用的操作系统平台,不同的计算机语言,不同的技术整合到一起。
调用webService需要导入jar包:ksoap2-android-assembly-3.4.0-jar-with-dependencies.jar包,这个包在网上可以下载,至于导入的方法 ,右键项目,选择最后一项properties-->Java build path-->Libraies-->Add external Jars   选择相应的路径下的jar文件就OK了,然后记得在Order and Export 里面将选择的jar文件勾选上。

 

AndroidManifest.xml 添加:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />   (特别留意要添加)


****************************************************************************

package com.example.ht;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

 


import java.io.IOException; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import org.xmlpull.v1.XmlPullParserException; 
 
//import android.os.Bundle; 
import android.os.Handler; 
import android.os.Looper; 
import android.os.Message; 
import android.os.StrictMode; 
import android.app.Activity; 
//import android.view.Menu; 
//import android.view.View; 
//import android.view.View.OnClickListener; 
//import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView;


public class F8_WS extends ActionBarActivity {

    private EditText et_phone; 
    private TextView tv_result; 
    private Button btn_query;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_f8__ws);
  
  initView();
 }
 
 ///////////////////////////
 private void initView() { 
        tv_result = (TextView) findViewById(R.id.tvResult); 
        et_phone = (EditText) findViewById(R.id.editText_f8_number); 
        btn_query = (Button) findViewById(R.id.btn_c1); 
        btn_query.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                String phone = et_phone.getText().toString().trim(); 
                //if ("".equals(phone) || phone.length() < 7) { 
                if ("".equals(phone)) { 
                    et_phone.setText("您输入为空"); 
                    et_phone.requestFocus(); 
                    tv_result.setText(""); 
                    return; 
                } 
                getRemoteInfo(phone); 
            } 
        }); 
    } 
 
    /**
     * 查询号码段归属地的方法
     * 
     * @param phone
     *            手机号码段
     */ 
    public void getRemoteInfo(final String phone) { 
        new Thread(new Runnable() { 
             
            @Override 
            public void run() { 
                // TODO Auto-generated method stub 
                // 命名空间 
                String nameSpace = "http://tempuri.org/"; 
                // 调用方法的名称 
                String methodName = "selectAllCargoInfor"; 
                // EndPoint 
                String endPoint = "http://192.168.1.91:8028/an_sql_test1.asmx"; 
                // SOAP Action 
                String soapAction = "http://192.168.1.91:8028/an_sql_test1.asmx/selectAllCargoInfor"; 
                // 指定WebService的命名空间和调用方法 
                SoapObject soapObject = new SoapObject(nameSpace, methodName); 
                // 设置需要调用WebService接口的两个参数mobileCode UserId 
                //soapObject.addProperty("mobileCode", phone); 
                //soapObject.addProperty("userId", ""); 
                // 生成调用WebService方法调用的soap信息,并且指定Soap版本 
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
                        SoapEnvelope.VER12); 
                envelope.bodyOut = soapObject; 
                // 是否调用DotNet开发的WebService 
                envelope.dotNet = true; 
                envelope.setOutputSoapObject(soapObject); 
                HttpTransportSE transport = new HttpTransportSE(endPoint); 
                try { 
                    transport.call(soapAction, envelope); 
                //} catch (IOException e) {
                } catch (Exception e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                }
               
                //catch (XmlPullParserException e) { 
                    // TODO Auto-generated catch block 
                    //e.printStackTrace(); 
                //} 
               
                // 获取返回的数据 
                SoapObject object = (SoapObject) envelope.bodyIn; 
                // 获取返回的结果 
                String result = object.getProperty(0).toString(); 
                Message message = handler.obtainMessage(); 
                message.obj = result; 
                handler.sendMessage(message); 
            } 
        }).start(); 
    } 
    private Handler handler = new Handler(){ 
        public void handleMessage(android.os.Message msg) { 
            // 将WebService得到的结果返回给TextView 
            tv_result.setText(msg.obj.toString()); 
        }; 
    };
 ///////////////////////////

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.f8__w, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

****************************************************************************

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ht.F8_WS" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/F8_Form" />

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="280dp"
        android:layout_height="300dp"
        android:layout_below="@+id/editText_f8_number"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:height="40dp"
        android:textSize="14sp" />

    <EditText
        android:id="@+id/editText_f8_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:hint="@string/editText_f8_no" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btn_c1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:text="@string/confirm" />

</RelativeLayout>
****************************************************************************

posted @ 2015-06-18 09:30  追逐苦痛  阅读(166)  评论(0编辑  收藏  举报