简体   繁体   English

如何在Android上使用Soap从Webservice发送和接收Hashmap

[英]How can i send and receive Hashmap from Webservice with Soap on Android

This is my code: 这是我的代码:

public class PostSoap extends AsyncTask<HashMap<String, String>, Void, HashMap<String, String> >{

    private String Tag= "PostSoap";
    final static String NAMESPACE = "http://wwyw.unimportant.com/";
    final static String METHOD_NAME = "unimportant";
    final static String SOAP_ACTION = "http://wwyw.adsdsad.com/unimportan";

    final static String URL = "http://unimportant:80/Service.asmx";

    @Override
    protected HashMap<String, String> doInBackground(
            HashMap<String, String>... params) {

        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
        request.addProperty("obj",params[0]);

        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        Log.i(Tag,"soapenv..");
        HttpTransportSE aht = new HttpTransportSE(URL);
        Log.i(Tag,"httptransport");

        try { aht.call(SOAP_ACTION, soapEnvelope);  }catch(Exception e){ e.printStackTrace(); return null; }

        try { return ""+?????????;}catch(Exception e){ e.printStackTrace(); return null; }

    }

     protected void onPostExecute(HashMap<bool,String> dyr) {
        if (dyr == null) { Toast.makeText(        , "error.", Toast.LENGTH_SHORT).show(); return; } // 

        try { Toast.makeText(         , " "+dyr, Toast.LENGTH_LONG).show(); }catch(Exception e){ } // 
    }
}

I know there are too many fail in this code. 我知道这段代码中有太多失败。 I want to send to request as a HashMap and also want to receive as a HashMap . 我想发送请求作为HashMap ,并希望作为HashMap接收。 How can I do it. 我该怎么做。 I need more advice about this. 我需要更多关于此的建议。
Thank you in advance:) 先感谢您:)

HashMap is serializable by itself,in case all Key and Value objects are serializable and in your case key and value objects are String objects. HashMap本身是可序列化的,以防所有Key和Value对象都是可序列化的,在您的情况下,key和value对象是String对象。 So you can serialize your HashMap to a byte array as follows: 因此,您可以将HashMap序列化为字节数组,如下所示:

 HashMap<String, String> hashMap; //reference to the HashMap to be serialized

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(hashMap);
        byte[] hashMapAsByteArray = bos.toByteArray(); //HashMap serialized to a  byte[] array
        oos.close();

You can convert this byte array to text with Base64 encoding and then embed it in a SOAP message. 您可以使用Base64编码将此字节数组转换为文本,然后将其嵌入到SOAP消息中。

您可以从hashmap创建JSON数组,并将该JSON数据发送到另一侧。

You can loop through the HashMap you got as input to add those key/values to SoapObject. 您可以循环访问作为输入的HashMap,将这些键/值添加到SoapObject。 After you recieve the response from the server, you need to parse the result according to the soap method's output format and fill them into a new HashMap or to an ArrayList object(depends on your needs) you create. 从服务器接收响应后,需要根据soap方法的输出格式解析结果,并将它们填充到新创建的HashMap或ArrayList对象(取决于您的需要)中。 If the server side of the application you create belongs to you, you can return a string which is formatted as XML or JSON. 如果您创建的应用程序的服务器端属于您,则可以返回格式为XML或JSON的字符串。 That would make it easier to parse into an object. 这样可以更容易地解析为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM