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,"查询数据为空!");
            }

        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,176评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,928评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 171,252评论 0 366
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,700评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,717评论 6 399
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,231评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,608评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,572评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,117评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,137评论 3 344
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,280评论 1 354
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,908评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,597评论 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,067评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,202评论 1 275
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,784评论 3 380
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,308评论 2 365

推荐阅读更多精彩内容