Android 调用 Webservice的实现过程

之前帮朋友做了一个服务端是Webservice的项目,把实现过程中的一下流程简单记录一下

1、开启Webservice服务

一般后台会开启一个服务给你或者为了直接方便调试的话,也可以自己用androidStudio 打开这个Webservice项目,如下图所示:


image.png

image.png
image.png

2、打开wsdl查看接口信息

image.png

image.png

image.png
<?xml version="1.0" encoding="UTF-8"?>

-<wsdl:definitions targetNamespace="http://service.linkfair.com/" name="WsService" xmlns:ns1="http://service.linkfair.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">


-<wsdl:types>


-<xs:schema targetNamespace="http://service.linkfair.com/" version="1.0" elementFormDefault="unqualified" xmlns:tns="http://service.linkfair.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Login" type="tns:Login"/>

<xs:element name="LoginResponse" type="tns:LoginResponse"/>

<xs:element name="LoginToUser" type="tns:LoginToUser"/>

<xs:element name="LoginToUserResponse" type="tns:LoginToUserResponse"/>

<xs:element name="insertInhouse" type="tns:insertInhouse"/>

<xs:element name="insertInhouseResponse" type="tns:insertInhouseResponse"/>

<xs:element name="listWarehouse" type="tns:listWarehouse"/>

<xs:element name="listWarehouseResponse" type="tns:listWarehouseResponse"/>

<xs:element name="scanMaterial" type="tns:scanMaterial"/>

<xs:element name="scanMaterialResponse" type="tns:scanMaterialResponse"/>


-<xs:complexType name="Login">


-<xs:sequence>

<xs:element name="arg0" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="LoginResponse">


-<xs:sequence>

<xs:element name="return" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="scanMaterial">


-<xs:sequence>

<xs:element name="arg0" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="scanMaterialResponse">


-<xs:sequence>

<xs:element name="return" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="insertInhouse">


-<xs:sequence>

<xs:element name="arg0" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="insertInhouseResponse">


-<xs:sequence>

<xs:element name="return" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="LoginToUser">


-<xs:sequence>

<xs:element name="arg0" type="xs:string" minOccurs="0"/>

<xs:element name="arg1" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="LoginToUserResponse">


-<xs:sequence>

<xs:element name="return" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>


-<xs:complexType name="listWarehouse">

<xs:sequence/>

</xs:complexType>


-<xs:complexType name="listWarehouseResponse">


-<xs:sequence>

<xs:element name="return" type="xs:string" minOccurs="0"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

</wsdl:types>


-<wsdl:message name="LoginResponse">

<wsdl:part name="parameters" element="ns1:LoginResponse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="scanMaterial">

<wsdl:part name="parameters" element="ns1:scanMaterial"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="insertInhouseResponse">

<wsdl:part name="parameters" element="ns1:insertInhouseResponse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="insertInhouse">

<wsdl:part name="parameters" element="ns1:insertInhouse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="LoginToUser">

<wsdl:part name="parameters" element="ns1:LoginToUser"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="Login">

<wsdl:part name="parameters" element="ns1:Login"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="scanMaterialResponse">

<wsdl:part name="parameters" element="ns1:scanMaterialResponse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="listWarehouseResponse">

<wsdl:part name="parameters" element="ns1:listWarehouseResponse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="listWarehouse">

<wsdl:part name="parameters" element="ns1:listWarehouse"> </wsdl:part>

</wsdl:message>


-<wsdl:message name="LoginToUserResponse">

<wsdl:part name="parameters" element="ns1:LoginToUserResponse"> </wsdl:part>

</wsdl:message>


-<wsdl:portType name="WsService">


-<wsdl:operation name="Login">

<wsdl:input name="Login" message="ns1:Login"> </wsdl:input>

<wsdl:output name="LoginResponse" message="ns1:LoginResponse"> </wsdl:output>

</wsdl:operation>


-<wsdl:operation name="scanMaterial">

<wsdl:input name="scanMaterial" message="ns1:scanMaterial"> </wsdl:input>

<wsdl:output name="scanMaterialResponse" message="ns1:scanMaterialResponse"> </wsdl:output>

</wsdl:operation>


-<wsdl:operation name="insertInhouse">

<wsdl:input name="insertInhouse" message="ns1:insertInhouse"> </wsdl:input>

<wsdl:output name="insertInhouseResponse" message="ns1:insertInhouseResponse"> </wsdl:output>

</wsdl:operation>


-<wsdl:operation name="LoginToUser">

<wsdl:input name="LoginToUser" message="ns1:LoginToUser"> </wsdl:input>

<wsdl:output name="LoginToUserResponse" message="ns1:LoginToUserResponse"> </wsdl:output>

</wsdl:operation>


-<wsdl:operation name="listWarehouse">

<wsdl:input name="listWarehouse" message="ns1:listWarehouse"> </wsdl:input>

<wsdl:output name="listWarehouseResponse" message="ns1:listWarehouseResponse"> </wsdl:output>

</wsdl:operation>

</wsdl:portType>

</wsdl:definitions>

基本入参都String,因为之前跟后台联调的时候发现传对象一直没成功,就折中改为传jsonObject的方式,后台再去转为对象

3、WebServiceHttpUtils 封装

/**
 * WebService android端调用
 * Created by Administrator on 2018/7/21.
 */

public class WebServiceHttpUtils {

    private static final String TAG = WebServiceHttpUtils.class.getSimpleName();

    /**
     * 调用webservice接口获取 SoapObject 对象
     * @param serviceNameSpace webservice的命名空间
     * @param serviceUrl webservice的请求地址 要带?wsdl
     * @param mathodName 调用webservice的函数名
     * @param isHasParameters 是否带参
     * @param parameters 参数集合,Map<Key(形参) ,Vlaue(实参)>
     * @return SoapObject 对象
     * @throws IOException
     * @throws XmlPullParserException
     */
    public static String call(String serviceNameSpace, String serviceUrl,
                                  String mathodName, boolean isHasParameters,
                                  Map<String, Object> parameters) {
        //要调用的方法名称
        SoapObject request = new SoapObject(serviceNameSpace, mathodName);
        //带参
        LogUtil.d(TAG,"isHasParameters========"+isHasParameters);
        if (isHasParameters) {
            if (parameters != null) {
                LogUtil.d(TAG,"parameters != null========");
                for (Map.Entry<String, Object> entry : parameters.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    LogUtil.d(TAG,"key========"+key);
                    LogUtil.d(TAG,"value======="+value);
                    if (!TextUtils.isEmpty(key) && value != null) {
                        // 设置参数
                        // 当这里的参数设置为arg0、arg1、arg2……argn的时候竟然是正确的。
                        request.addProperty(key, value);
                    }
                }
            }else {
                LogUtil.d(TAG,"parameters == null========");
            }
        }else {

        }
        //创建SoapSerializationEnvelope 对象,同时指定soap版本号(之前在wsdl中看到的)
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求,所以是设置bodyOut
        //        envelope.dotNet = true;//由于是.net开发的webservice,所以这里要设置为true

        HttpTransportSE httpTransportSE = new HttpTransportSE(serviceUrl);
        try {
            httpTransportSE.call(null, envelope);//调用
            // 获取返回的数据
            SoapObject soapObject = (SoapObject) envelope.bodyIn;
            String respoend = soapObject.getProperty(0).toString();
            Log.d(TAG,"接口-"+mathodName+"-请求结果:"+respoend);
            return respoend;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }


        return "";
    }
}

4、BaseApplication封装

/**
 *
 * Created by psl on 2017/5/7.
 */

public class BaseApplication extends MultiDexApplication {

    public static Context context;
    private static RetrofitAPIImpl mRetrofitAPI;
    private static Retrofit mRetrofit;
    private static BaseApplication sInstence;

    // 命名空间
    public static final String nameSpace = "http://service.linkfair.com/";

    // serviceUrl
    public static final String serviceUrl = "http://192.168.4.221:80/8888/linkfair?wsdl";


    public static BaseApplication getInstence() {
        return sInstence;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstence = this;
        context = this;

    }


}

5、无参调用方式

        showLoading();
        //仓库   启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
        QueryListWarehouseTask queryListWarehouseTask = new QueryListWarehouseTask();
        //启动后台任务
        queryListWarehouseTask.execute();


 /**
     * 选择仓库
     */
    class QueryListWarehouseTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            String result = WebServiceHttpUtils.call(BaseApplication.getInstence().nameSpace,BaseApplication.getInstence().serviceUrl,LISTWAREHOUSE_METHODNAME,false,null);
            LogUtil.d(TAG,"result========"+result);
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            dismissLoading();
            // 将WebService返回的结果显示在TextView中
            Log.d(TAG,"onPostExecute============result==============="+result);

            if (!TextUtils.isEmpty(result)){
                Gson gson = new Gson();
                List<WareHouseBean> wareHouseBeanList = gson.fromJson(result,new TypeToken<List<WareHouseBean>>(){}.getType());
                if (wareHouseBeanList != null && wareHouseBeanList.size() > 0){
                    mTestSelectArr = new String[wareHouseBeanList.size()];

                    for (int i = 0; i < wareHouseBeanList.size(); i++) {
                        WareHouseBean wareHouseBean = wareHouseBeanList.get(i);
                        mTestSelectArr[i] = wareHouseBean.getName();
                        mWareHouseList.add(wareHouseBean);
                    }

                }else {

                }
            }
        }
    }

6、有参调用方式

            showLoading();
            //启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
            QueryCcanMaterialTask queryyCcanMaterialTask = new QueryCcanMaterialTask();
            //启动后台任务
            queryyCcanMaterialTask.execute("123456");

  /**
     * 扫码得到物料
     */
    class QueryCcanMaterialTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            Map<String,Object> map = new HashMap<>();
            map.put("arg0",params[0]);
            String result = WebServiceHttpUtils.call(BaseApplication.getInstence().nameSpace,BaseApplication.getInstence().serviceUrl,SCANMATERIAL_METHODNAME,true,map);
            LogUtil.d(TAG,"result========"+result);
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            dismissLoading();
            // 将WebService返回的结果显示在TextView中
            Log.d(TAG,"onPostExecute============result==============="+result);

            if (!TextUtils.isEmpty(result)){
                Gson gson = new Gson();
                ScanMaterialBean scanMaterialBean = gson.fromJson(result,ScanMaterialBean.class);
                if (scanMaterialBean != null){

                    if (scanMaterialBean.isSuccess()){
                        if (scanMaterialBean.getContent() != null){
                            list.add(scanMaterialBean.getContent());
                            adapter.notifyDataSetChanged();
                            rvList.scrollToPosition(list.size()-1);
                        }
                    }else {

                    }

                }else {
                    ToastUtil.toast(context,"查询数据为空!");
                }
            }
        }
    }

7、把入参List<T>改为JsonArray的调用方式

 /**
     * 处理提交数据
     */
    private void delComfirmData(){
        //提交材料  下面是提交类子
        // 真实使用请按照下面的格式遍历列表adapter的列表中的数据(ArrayList<ScanMaterialBean.ContentBean> list)
        //就是把遍历(ArrayList<ScanMaterialBean.ContentBean> list)  添加到
        List<InWarehouse> InWarehouseList = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                ScanMaterialBean.ContentBean contentBean = list.get(i);
                InWarehouse mInWarehouse = new InWarehouse();
                mInWarehouse.id = mWareHouseId;
                mInWarehouse.materialId = contentBean.getId();
                mInWarehouse.taskCount = 1;  //假数据  需要根据项目填真实数据
                mInWarehouse.putCount = 1; //假数据  需要根据项目填真实数据
                mInWarehouse.name = contentBean.getName();
                mInWarehouse.model =contentBean.getModel();

                InWarehouseList.add(mInWarehouse);
            }

            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            JSONObject tmpObj = null;
            int count = InWarehouseList.size();
            for(int i = 0; i < count; i++)
            {
                InWarehouse inWarehouse = InWarehouseList.get(i);
                tmpObj = new JSONObject();
                try {
                    tmpObj.put("id" ,inWarehouse.id);
                    tmpObj.put("materialId",inWarehouse.id);
                    tmpObj.put("taskCount", inWarehouse.id);
                    tmpObj.put("putCount" ,inWarehouse.id);
                    tmpObj.put("name", inWarehouse.id);
                    tmpObj.put("model",inWarehouse.id);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                jsonArray.put(tmpObj);
                tmpObj = null;
            }
            String personInfos = jsonArray.toString(); // 将JSONArray转换得到String

            showLoading();
            //启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
            QueryInsertInhouseTask queryListWarehouseTask = new QueryInsertInhouseTask();
            //启动后台任务
            queryListWarehouseTask.execute(personInfos);
        }

    }

 /**
     * 仓库录入
     */
    class QueryInsertInhouseTask extends AsyncTask<String, String, String> {


        @Override
        protected String doInBackground(String... strings) {
            Map<String,Object> map = new HashMap<>();
            map.put("arg0",strings[0]);
            String result = WebServiceHttpUtils.call(BaseApplication.getInstence().nameSpace,BaseApplication.getInstence().serviceUrl,INSERTINHOUSE_METHODNAME,true,map);
            LogUtil.d(TAG,"result========"+result);
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            dismissLoading();
            // 将WebService返回的结果显示在TextView中
            Log.d(TAG,"onPostExecute============result==============="+result);
            Gson gson = new Gson();
            InsertInhouseBean mInsertInhouseBean = gson.fromJson(result,InsertInhouseBean.class);
            if (mInsertInhouseBean != null){

                ToastUtil.toast(context,!TextUtils.isEmpty(mInsertInhouseBean.message)?mInsertInhouseBean.message:"");

            }else {
                ToastUtil.toast(context,"查询数据为空!");
            }

        }
    }

8、完整代码

/**
 * 采购入库
 */
public class PurchaseAndStorageActivity extends BaseActivity {

    private static final String TAG = PurchaseAndStorageActivity.class.getSimpleName();
    private final static String SCAN_ACTION = ScanManager.ACTION_DECODE;//default action
    private ActionBar actionBar;
    @BindView(R.id.title_bar)
    TitleLayout titleBar;
    @BindView(R.id.tv_storage_title)
    TextView tvStorageTitle;
    @BindView(R.id.tv_storage)
    TextView tvStorage;
    @BindView(R.id.bn_select)
    Button bnSelect;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.bn_comfirm)
    Button bnComfirm;
    @BindView(R.id.bn_cancel)
    Button bnCancel;
    @BindView(R.id.et_scan)
    EditText etScan;
    private Context mContext;
    private Unbinder unbinder;
    private SelectPopupWindow selectPopupWindow;
    private Vibrator mVibrator;
    private ScanManager mScanManager;
    private SoundPool soundpool = null;
    private int soundid;
    private String barcodeStr;
    private boolean isScaning = false;

    private String[] mTestSelectArr = null;
    private ArrayList<WareHouseBean> mWareHouseList = new ArrayList<>();

    // 查询仓库调用的方法名称
    private static final String LISTWAREHOUSE_METHODNAME = "listWarehouse";

    // 扫码得到物料调用的方法名称
    private static final String SCANMATERIAL_METHODNAME = "scanMaterial";

    // 仓库录入调用的方法名称
    private static final String INSERTINHOUSE_METHODNAME = "insertInhouse";
    private PurchaseAndStorageAdapter adapter;

    private ArrayList<ScanMaterialBean.ContentBean> list;

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            isScaning = false;
            soundpool.play(soundid, 1, 1, 0, 0, 1);
            mVibrator.vibrate(100);

            byte[] barcode = intent.getByteArrayExtra(ScanManager.DECODE_DATA_TAG);
            int barcodelen = intent.getIntExtra(ScanManager.BARCODE_LENGTH_TAG, 0);
            byte temp = intent.getByteExtra(ScanManager.BARCODE_TYPE_TAG, (byte) 0);
            android.util.Log.i("debug", "----codetype--" + temp);
            barcodeStr = new String(barcode, 0, barcodelen);
            etScan.append(" length:"  +barcodelen);
            etScan.append(" barcode:"  +barcodeStr);
            etScan.setText(barcodeStr);

            showLoading();
            //启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
            QueryCcanMaterialTask queryyCcanMaterialTask = new QueryCcanMaterialTask();
            //启动后台任务
            queryyCcanMaterialTask.execute("123456");
        }

    };
    private int mWareHouseId;


    @Override
    public int setLayout() {
        return R.layout.activity_purchase_and_storage;

    }


    @Override
    public void initUI() {
        mContext = this;
        unbinder = ButterKnife.bind(this);
        etScan.setFocusable(false);
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {
        adapter = new PurchaseAndStorageAdapter(mContext,list);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(mContext);
        rvList.setLayoutManager(mLinearLayoutManager);
        rvList.setAdapter(adapter);
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        mWareHouseList.clear();
        initRecyclerView();
    }

    @Override
    public void addListeners() {
        bnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTestSelectArr == null || mTestSelectArr.length == 0){
                    ToastUtil.toast(context,"仓库数据为空!");
                }else {
                    initSelect();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        //数据提交按钮
        if (v.equals(bnComfirm)){
            delComfirmData();
        }else if (v.equals(bnCancel)){
            finish();
        }
    }

    /**
     * 处理提交数据
     */
    private void delComfirmData(){
        //提交材料  下面是提交类子
        // 真实使用请按照下面的格式遍历列表adapter的列表中的数据(ArrayList<ScanMaterialBean.ContentBean> list)
        //就是把遍历(ArrayList<ScanMaterialBean.ContentBean> list)  添加到
        List<InWarehouse> InWarehouseList = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                ScanMaterialBean.ContentBean contentBean = list.get(i);
                InWarehouse mInWarehouse = new InWarehouse();
                mInWarehouse.id = mWareHouseId;
                mInWarehouse.materialId = contentBean.getId();
                mInWarehouse.taskCount = 1;  //假数据  需要根据项目填真实数据
                mInWarehouse.putCount = 1; //假数据  需要根据项目填真实数据
                mInWarehouse.name = contentBean.getName();
                mInWarehouse.model =contentBean.getModel();

                InWarehouseList.add(mInWarehouse);
            }

            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            JSONObject tmpObj = null;
            int count = InWarehouseList.size();
            for(int i = 0; i < count; i++)
            {
                InWarehouse inWarehouse = InWarehouseList.get(i);
                tmpObj = new JSONObject();
                try {
                    tmpObj.put("id" ,inWarehouse.id);
                    tmpObj.put("materialId",inWarehouse.id);
                    tmpObj.put("taskCount", inWarehouse.id);
                    tmpObj.put("putCount" ,inWarehouse.id);
                    tmpObj.put("name", inWarehouse.id);
                    tmpObj.put("model",inWarehouse.id);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                jsonArray.put(tmpObj);
                tmpObj = null;
            }
            String personInfos = jsonArray.toString(); // 将JSONArray转换得到String

            showLoading();
            //启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
            QueryInsertInhouseTask queryListWarehouseTask = new QueryInsertInhouseTask();
            //启动后台任务
            queryListWarehouseTask.execute(personInfos);
        }

    }

    private void initSelect() {
        //实例化SelectPicPopupWindow

        selectPopupWindow = new SelectPopupWindow(PurchaseAndStorageActivity.this, mTestSelectArr, new SelectPopupWindow.ItemClickPositionListener() {
            @Override
            public void getItemClickPosition(int position) {
                tvStorage.setText(mTestSelectArr[position]);
                WareHouseBean wareHouseBean = mWareHouseList.get(position);
                mWareHouseId = wareHouseBean.getId();
            }
        });

        //显示窗口
        selectPopupWindow.showAtLocation(bnSelect, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
    }



    private void initScan() {
        // TODO Auto-generated method stub
        mScanManager = new ScanManager();
        mScanManager.openScanner();

        mScanManager.switchOutputMode( 0);
        soundpool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 100); // MODE_RINGTONE
        soundid = soundpool.load("/etc/Scan_new.ogg", 1);
    }



    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
        etScan.setText("");
        IntentFilter filter = new IntentFilter();
        int[] idbuf = new int[]{PropertyID.WEDGE_INTENT_ACTION_NAME, PropertyID.WEDGE_INTENT_DATA_STRING_TAG};
        String[] value_buf = mScanManager.getParameterString(idbuf);
        if(value_buf != null && value_buf[0] != null && !value_buf[0].equals("")) {
            filter.addAction(value_buf[0]);
        } else {
            filter.addAction(SCAN_ACTION);
        }

        registerReceiver(mScanReceiver, filter);

        showLoading();
        //仓库   启动后台异步线程进行连接webService操作,并且根据返回结果在主线程中改变UI
        QueryListWarehouseTask queryListWarehouseTask = new QueryListWarehouseTask();
        //启动后台任务
        queryListWarehouseTask.execute();

    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if(mScanManager != null) {
            mScanManager.stopDecode();
            isScaning = false;
        }
        unregisterReceiver(mScanReceiver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }


    /**
     * 选择仓库
     */
    class QueryListWarehouseTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            String result = WebServiceHttpUtils.call(BaseApplication.getInstence().nameSpace,BaseApplication.getInstence().serviceUrl,LISTWAREHOUSE_METHODNAME,false,null);
            LogUtil.d(TAG,"result========"+result);
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            dismissLoading();
            // 将WebService返回的结果显示在TextView中
            Log.d(TAG,"onPostExecute============result==============="+result);

            if (!TextUtils.isEmpty(result)){
                Gson gson = new Gson();
                List<WareHouseBean> wareHouseBeanList = gson.fromJson(result,new TypeToken<List<WareHouseBean>>(){}.getType());
                if (wareHouseBeanList != null && wareHouseBeanList.size() > 0){
                    mTestSelectArr = new String[wareHouseBeanList.size()];

                    for (int i = 0; i < wareHouseBeanList.size(); i++) {
                        WareHouseBean wareHouseBean = wareHouseBeanList.get(i);
                        mTestSelectArr[i] = wareHouseBean.getName();
                        mWareHouseList.add(wareHouseBean);
                    }

                }else {

                }
            }
        }
    }


    /**
     * 扫码得到物料
     */
    class QueryCcanMaterialTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

            Map<String,Object> map = new HashMap<>();
            map.put("arg0",params[0]);
            String result = WebServiceHttpUtils.call(BaseApplication.getInstence().nameSpace,BaseApplication.getInstence().serviceUrl,SCANMATERIAL_METHODNAME,true,map);
            LogUtil.d(TAG,"result========"+result);
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            dismissLoading();
            // 将WebService返回的结果显示在TextView中
            Log.d(TAG,"onPostExecute============result==============="+result);

            if (!TextUtils.isEmpty(result)){
                Gson gson = new Gson();
                ScanMaterialBean scanMaterialBean = gson.fromJson(result,ScanMaterialBean.class);
                if (scanMaterialBean != null){

                    if (scanMaterialBean.isSuccess()){
                        if (scanMaterialBean.getContent() != null){
                            list.add(scanMaterialBean.getContent());
                            adapter.notifyDataSetChanged();
                            rvList.scrollToPosition(list.size()-1);
                        }
                    }else {

                    }

                }else {
                    ToastUtil.toast(context,"查询数据为空!");
                }
            }
        }
    }


    /**
     * 仓库录入
     */
    class QueryInsertInhouseTask extends AsyncTask<String, String, String> {


        @Override
        protected String doInBackground(String... strings) {
            Map<String,Object> map = new HashMap<>();
            map.put("arg0",strings[0]);
            String result = WebServiceHttpUtils.call(BaseApplication.getInstence().nameSpace,BaseApplication.getInstence().serviceUrl,INSERTINHOUSE_METHODNAME,true,map);
            LogUtil.d(TAG,"result========"+result);
            return result;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            dismissLoading();
            // 将WebService返回的结果显示在TextView中
            Log.d(TAG,"onPostExecute============result==============="+result);
            Gson gson = new Gson();
            InsertInhouseBean mInsertInhouseBean = gson.fromJson(result,InsertInhouseBean.class);
            if (mInsertInhouseBean != null){

                ToastUtil.toast(context,!TextUtils.isEmpty(mInsertInhouseBean.message)?mInsertInhouseBean.message:"");

            }else {
                ToastUtil.toast(context,"查询数据为空!");
            }

        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容