|
2#
楼主 |
发表于 2018-6-20 15:37:57
|
只看该作者
- <p>public class RemoteDataHandler {</p><p>
- </p><p> private static Map<String,SoapObject> sso = new HashMap<String, SoapObject>();</p><p>
- </p><p> private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(6,</p><p> 30, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());</p><p>
- </p><p> public interface CallBackXml {</p><p> public void dataLoaded(SoapObject soapObject);</p><p> }</p><p>
- </p><p> /**</p><p> *</p><p> * @param activity BaseActivity</p><p> * @param methodname 方法名</p><p> * @param rpc 参数</p><p> * @param callback 回调</p><p> * @param showProgress 是否显示对话框</p><p> */</p><p> public static void asyncSoap(final BaseActivity activity, final String methodname, final SoapObject rpc, fi</p><p>nal CallBackXml callback, final boolean showProgress){</p><p> if (showProgress)</p><p> activity.showProgress();</p><p> final Handler handler = new Handler() {</p><p> @Override</p><p> public void handleMessage(Message msg) {</p><p> switch (msg.what){</p><p> case 0:</p><p> activity.closeProgress();</p><p> SoapObject soapObject = sso.get(msg.obj);</p><p> sso.clear();</p><p> callback.dataLoaded(soapObject);</p><p> }</p><p> }</p><p> };</p><p>
- </p><p> threadPool.execute(new Runnable() {</p><p> @Override</p><p> public void run() {</p><p> String nameSpace = Constants.NAME_SPACE; //命名空间</p><p> String endPoint = Constants.END_POINT;</p><p> String soapAction = nameSpace+methodname; //Action</p><p> // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本</p><p> SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);</p><p> envelope.bodyOut = rpc;</p><p> // 设置是否调用的是dotNet开发的WebService</p><p> envelope.dotNet = true;</p><p> // 等价于envelope.bodyOut = rpc;</p><p> envelope.setOutputSoapObject(rpc);</p><p> HttpTransportSE transport = new HttpTransportSE(endPoint);</p><p> try {</p><p> // 调用WebService</p><p> transport.call(soapAction, envelope);</p><p> } catch (Exception e) {</p><p> e.printStackTrace();</p><p> }</p><p> SoapObject object = (SoapObject) envelope.bodyIn;</p><p> Log.i("soap","object : "+object+"");</p><p> ////////////////////////////////////////////////////////</p><p> sso.put(methodname, object);</p><p> Message msg = handler.obtainMessage(0,methodname);</p><p> handler.sendMessage(msg);</p><p> }</p><p> });</p><p>
- </p><p> }</p><p>
- </p><p> /**</p><p> *</p><p> * @param fragment BaseFragment</p><p> * @param methodname 方法名</p><p> * @param rpc 参数</p><p> * @param callback 回调</p><p> * @param showProgress 是否显示对话框</p><p> */</p><p> public static void asyncSoapfromfragment(final BaseFragment fragment, final String methodname, final S</p><p>oapObject rpc, final CallBackXml callback, final boolean showProgress){</p><p> if (showProgress)</p><p> TrainApplication.getCoreApplication().showProgressDialog(fragment.getContext());</p><p> final Handler handler = new Handler() {</p><p> @Override</p><p> public void handleMessage(Message msg) {</p><p> switch (msg.what){</p><p> case 0:</p><p> TrainApplication.getCoreApplication().dismissProgressDialog();</p><p> SoapObject soapObject = sso.get(msg.obj);</p><p> callback.dataLoaded(soapObject);</p><p> sso.clear();</p><p> }</p><p> }</p><p> };</p><p>
- </p><p> threadPool.execute(new Runnable() {</p><p> @Override</p><p> public void run() {</p><p> String nameSpace = Constants.NAME_SPACE; //命名空间</p><p> String endPoint = Constants.END_POINT;</p><p> String soapAction = nameSpace+methodname; </p><p> // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本</p><p> SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);</p><p> envelope.bodyOut = rpc;</p><p> // 设置是否调用的是dotNet开发的WebService</p><p> envelope.dotNet = true;</p><p> // 等价于envelope.bodyOut = rpc;</p><p> envelope.setOutputSoapObject(rpc);</p><p> HttpTransportSE transport = new HttpTransportSE(endPoint);</p><p> try {</p><p> // 调用WebService</p><p> transport.call(soapAction, envelope);</p><p> } catch (Exception e) {</p><p> e.printStackTrace();</p><p> }</p><p> SoapObject object = (SoapObject) envelope.bodyIn;</p><p> ////////////////////////////////////////////////////////</p><p> sso.put(methodname, object);</p><p> Message msg = handler.obtainMessage(0,methodname);</p><p> handler.sendMessage(msg);</p><p> }</p><p> });</p><p> }</p><p>}</p><p>这里就是soap请求webservice的方法。</p><p>我们在使用的时候:</p><p>
- </p><p> private void login(){</p><p> if(TextUtils.isEmpty(username.getText().toString().trim())){</p><p> TrainToast.makeText(LoginActivity.this, getResources().getString(R.string.error_empty_username), </p><p>Toast.LENGTH_SHORT).show();</p><p> return;</p><p> }</p><p> if(TextUtils.isEmpty(password.getText().toString().trim())){</p><p> TrainToast.makeText(this, getResources().getString(R.string.error_empty_password), Toast.LENG</p><p>TH_SHORT).show();</p><p> return;</p><p> }</p><p> SoapObject rpc = new SoapObject(Constants.NAME_SPACE, Constants.METHOD_LOGIN);</p><p> rpc.addProperty("username", username.getText().toString().trim());</p><p> rpc.addProperty("pass", password.getText().toString().trim());</p><p> RemoteDataHandler.asyncSoap(LoginActivity.this, Constants.METHOD_LOGIN, rpc, new RemoteDataH</p><p>andler.CallBackXml() {</p><p> @Override</p><p> public void dataLoaded(SoapObject soapObject) {</p><p> if (soapObject != null && soapObject.getPropertyCount() > 0) {</p><p> String result = soapObject.getPropertyAsString(0);</p><p> if(result!=null){</p><p> StuffEntity stuffEntity = GsonUtil.json2bean(result, StuffEntity.class);</p><p> if(stuffEntity!=null){</p><p> int code = stuffEntity.getCode();</p><p> if(code==1){</p><p> //进行登录操作</p><p> }else{</p><p> TrainToast.makeText(LoginActivity.this,stuffEntity.getMsg(), Toast.LENGTH_SHORT).show();</p><p> }</p><p> }else{</p><p> TrainToast.makeText(LoginActivity.this,getResources().getString(R.string.error_service), Toast.LENGTH_SHORT).show();</p><p> }</p><p> }else{</p><p> TrainToast.makeText(LoginActivity.this,getResources().getString(R.string.error_service), Toast.LENGTH_SHORT).show();</p><p> }</p><p> } else {</p><p> TrainToast.makeText(LoginActivity.this, getResources().getString(R.string.error_net), Toast.LENGTH_SHORT).show();</p><p> }</p><p> }</p><p> }, true);</p><p> }</p>
复制代码
这样我们就很熟悉了,拿到了结果不需要再进行复杂的xml解析,直接取到soapobject其中的第一条strin
g,便是我们需要的json,然后直接用gson解析到bean类里面。这样我们便成功的用android与webservic
e进行通讯并拿到了json的结果。
|
|