简体   繁体   中英

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 . 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. So you can serialize your HashMap to a byte array as follows:

 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.

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

You can loop through the HashMap you got as input to add those key/values to 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. If the server side of the application you create belongs to you, you can return a string which is formatted as XML or JSON. That would make it easier to parse into an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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