Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習之——(4)項目中的調用WebService學習

Android學習之——(4)項目中的調用WebService學習

編輯:關於Android編程


public void DoTuiKuDJ(final String dtZzjl, final String dtRkdmx,
        final String dtRkd) {//這裡的三個String都是將類放到List當中,然後將List轉化成XML的形式來傳遞的,List轉化XML的代碼下面會給出
    new Webservice(context, handler, TuiKuDJ, true, null, null, null) {
        @Override
        public void addProgerty(SoapObject rpc) {
                    // TODO Auto-generated method stub
            method = 2;
            rpc.addProperty(dtZzjl, dtZzjl);
            rpc.addProperty(dtRkdmx, dtRkdmx);
            rpc.addProperty(dtRkd, dtRkd);
        }
    };
}
/**
* 數據傳回來處理
*/
private Handler handler = new Handler() {
    @SuppressWarnings(unchecked)
    public void handleMessage(android.os.Message msg) {
        switch (msg.what) {
        case 0:
            if (method == 1) {
                zzjl = (List) (msg.obj);//msg.obj為傳回來的數據
                tuiKuFragment.DealZzjl();
                break;
            }
                break;
            break;
        }
        };
};
WebService類:
/**
 * web service通信類
 * @author Administrator 上傳時要分int,String 而接受數據射計類都要是String
 */
public abstract class Webservice {
    public static final String NAMESPACE = http://tempuri.org/;
    // WebService地址
    private String URL;
    /**
     * web service通信
     * @param context
     * @param handler
     * @param methodName
     *            方法名
     * @param flag
     *            true:get data that should be resolved ;false:send data
     * @param cla
     *            The class to be reflected
     * @param entityname
     *            需要轉換成類的包名string
     */
    @SuppressWarnings(rawtypes)
    public Webservice(Context context, Handler handler, String methodName,
            boolean flag, Class cla, ParseData parse, String entityname) {
        URL = http://
                + context.getSharedPreferences(ServiceIP,
                        Context.MODE_PRIVATE).getString(IP,
                        127.0.0.1/EOIISService.asmx);
        tongXun(context, handler, methodName, flag, cla, parse, entityname);
    }
    /**
     * 以webservice獲取數據
     */
    @SuppressWarnings(rawtypes)
    private void tongXun(final Context context, final Handler handler,
            final String methodName, final boolean flag, final Class cla,
            final ParseData parse, final String entityname) {
        Thread thread = new Thread() {
            public void run() {
                webServiceThread(context, handler, methodName, flag, cla,
                        parse, entityname);
            }
        };
        thread.start();
        // thread.
    }
    /**
     * 設置webservice要傳遞的屬性
     */
    public abstract void addProgerty(SoapObject rpc);
    /**
     * webservice 線程
     * 
     * @param context
     * @param handler
     * @param methodName
     * @param flag
     *            true:get data that should be resolved ;false:send data
     * @param cla
     *            The class to be reflected * @param entityname 需要轉換成類的包名string
     */
    @SuppressWarnings(rawtypes)
    public void webServiceThread(final Context context, final Handler handler,
            String methodName, boolean flag, Class cla, final ParseData parse,
            String entityname) {
        try {
            // System.out.println(rpc------);
            // 指定webservice的命名空間和調用的方法名
            Object obj1 = null;
            SoapObject rpc = new SoapObject(NAMESPACE, methodName);
            addProgerty(rpc);
            MyHttptran ht = new MyHttptran(URL, 5000);
            ht.debug = true;
            // 生成調用Webservice方法的SOAP請求信息.該信息由SoapSerializationEnvelope對象描述
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.bodyOut = rpc;
            envelope.dotNet = true;
            envelope.setOutputSoapObject(rpc);
            ht.call(NAMESPACE + methodName, envelope);
            if (envelope.bodyIn.toString().startsWith(SoapFault)) {
                SoapFault sf = (SoapFault) (envelope.bodyIn);
                Log.i(fault, sf.faultstring);
                if (handler != null) {
                    Message message = Message.obtain(handler);
                    message.what = 3;
                    message.obj = sf.faultstring;
                    message.sendToTarget();
                }
                return;
            }
            // 得到返回結果
            Log.i(bodyIn, envelope.bodyIn.toString());
            SoapObject result = (SoapObject) envelope.bodyIn;
            if (result.toString().equals(methodName + Response{})) {
                // pd = false;
                if (null != handler) {
                    // send message to the main thread
                    Message message = Message.obtain(handler);
                    // message.obj = year;
                    message.what = 1;
                    message.sendToTarget();
                }
                return;
            }
            if (parse == null) {
                if (null != handler) {
                    Message message = Message.obtain(handler);
                    message.obj = result.getProperty(methodName + Result)
                            .toString();
                    message.what = 1;
                    message.sendToTarget();
                }
                return;
            }
            SoapObject detail = (SoapObject) result.getProperty(methodName
                    + Result);
            if (detail.toString().equals(anyType{})) {
                if (null != handler) {
                    Message message = Message.obtain(handler);
                    message.what = 2;
                    message.sendToTarget();
                }
            } else {
                obj1 = parse.doing(detail, cla, entityname);
                // 發送消息回主線程
                if (null != handler) {
                    Message message = Message.obtain(handler);
                    message.obj = obj1;
                    message.what = 0;
                    message.sendToTarget();
                }
            }
        } catch (ConnectException e) {
            e.printStackTrace();
            if (null != handler) {
                handler.sendEmptyMessage(-1);
            }
        } catch (SocketTimeoutException e) {
            // TODO: handle exception
            e.printStackTrace();
            if (null != handler) {
                handler.sendEmptyMessage(-1);
            }
        } catch (XmlPullParserException e) {
            // TODO: handle exception
            e.printStackTrace();
            if (null != handler) {
                handler.sendEmptyMessage(-1);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            if (null != handler) {
                Message message = Message.obtain(handler);
                message.obj = e.getMessage();
                message.what = -2;
                message.sendToTarget();
            }
        }
    }
}
關於將List
/**
 * 用於制造xml類型的字符串
 * @author Administrator syc
 * @since 2012-10-31
 */
public class CreateXml {
    public String createXml(@SuppressWarnings(rawtypes) List list,
            String tableName) {
        String xml = ;
        StringWriter xmlWriter = new StringWriter();
        XmlSerializer xmlSerializer = Xml.newSerializer();
        try {
            xmlSerializer.setOutput(xmlWriter);
            xmlSerializer.startDocument(UTF-16, null);
            xmlSerializer.startTag(, NewDataSet);
            // DT
            if (null != list) {
                FanShe reflect = new FanShe();
                List shuxingList = reflect.getPropertyNames(list.get(0)
                        .getClass());
                for (Object object : list) {
                    xmlSerializer.startTag(, object.getClass().getName());
                    @SuppressWarnings(unchecked)
                    List vlaueList = reflect.getValueList(shuxingList,
                            object);
                    for (int i = 0; i < shuxingList.size(); i++) {
                        xmlSerializer.startTag(, shuxingList.get(i));
                        if (vlaueList.get(i) == null) {
                            xmlSerializer.text();
                        } else {
                            xmlSerializer.text(vlaueList.get(i));
                        }
                        xmlSerializer.endTag(, shuxingList.get(i));
                    }
                    xmlSerializer.endTag(, object.getClass().getName());
                }
                xmlSerializer.endTag(, NewDataSet);
                // 屬性集合
                xmlSerializer.endDocument();
                xmlSerializer.flush();
                // xmlWriter.flush();
                xml = xmlWriter.toString().split(\?>)[1];
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return xml;
    }
}
轉化為XML調用方式如下:
qiXieZzjlEntitieString = new CreateXml().createXml(qiXieZzjlEntities,qiXieZzjlEntities);
//qiXieZzjlEntities聲明如下:private List qiXieZzjlEntities = new ArrayList();     
//qiXieZzjlEntities字符串名字可以任意 



Mr.傅:學習筆記
歡迎轉載,轉載注明出處,謝謝

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved