如何使用soap解析<strxml> String </strxml>?

时间:2013-05-13 09:41:45

标签: android ksoap2 android-ksoap2

这是我的SOAP webservice.i有donn。它有错误。这是我的代码。任何人都可以帮我。如何解析android中的字符串。       INT       串       INT       INT       串       串      提前谢谢。

public boolean callwebservice() {

    boolean result = false;
    int patid = 1;
    String xml = "";

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("PatientId", patid);

    request.addProperty("AppointMentDate", appdate);
    request.addProperty("TimeFrom", apptimeto);
    request.addProperty("TimeTo", appfrom);
    request.addProperty("ReasonForAppointMent", appreason);
    request.addProperty("strxml", xml);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransport = new HttpTransportSE(URL);
    httpTransport.debug = true;
    try {

        httpTransport.call(SOAP_ACTION, envelope);
        httpTransport
                .setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        // SoapObject response = (SoapObject) envelope.bodyIn;
        SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

        System.out.println("myApp" + response.toString());
        System.out.println("response" + response);

        if (response.toString().equalsIgnoreCase("false")) {
            result = true;

        }

    } catch (SocketException ex) {
        System.out.println("Error : " + "Error on soapPrimitiveData() "
                + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception e) {
        System.out.println("Error : " + "Error on soapPrimitiveData() "
                + e.getMessage());
        e.printStackTrace();
    }
    return result;

1 个答案:

答案 0 :(得分:0)

首先将ksoap2库导入您的项目并尝试此操作。Here is the link

 public class MainActivity extends Activity {
private final String NAMESPACE = "http://www.webserviceX.NET/";
private final String URL = "http://www.webservicex.net/CurrencyConvertor.asmx";
private final String SOAP_ACTION = "http://www.webserviceX.NET/ConversionRate";
private final String METHOD_NAME = "ConversionRate";
SoapObject request;
String weight;
String fromUnit;
String toUnit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    request = new SoapObject(NAMESPACE, METHOD_NAME); 

    weight = "USD";
    fromUnit = "INR";
    toUnit = "Kilograms";

    PropertyInfo weightProp =new PropertyInfo();
    weightProp.setName("FromCurrency");
    weightProp.setValue(weight);
    weightProp.setType(String.class);
    request.addProperty(weightProp);

    PropertyInfo fromProp =new PropertyInfo();
    fromProp.setName("ToCurrency");
    fromProp.setValue(fromUnit);
    fromProp.setType(String.class);
    request.addProperty(fromProp);



    //calling the AsyncTask
    new Task().execute();


}
class Task extends AsyncTask<Void, Void, Void>{
    SoapPrimitive response;
    @Override
    protected Void doInBackground(Void... params) {
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", ""+response);



        } catch (Exception e) {
            e.printStackTrace();
        }
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        TextView tv = new TextView(getApplicationContext());
        tv.setText("1"+weight+" equals "+response.toString()+ " "+fromUnit);
        tv.setTextSize(50);
        setContentView(tv);
        super.onPostExecute(result);
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

相关问题