2
  1. 此代码没有给我任何错误或警告,但无法正常工作,我不知道问题出在哪里,请帮助我。

    package com.example.webtest;
    
    import org.ksoap2.SoapEnvelope;
    import android.util.Log;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    
    String UserFahrenheit;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    Button b=(Button) findViewById(R.id.button);
    final EditText Med=(EditText) findViewById(R.id.MedServTextView);
    final TextView Test=(TextView) findViewById(R.id.TestTextView);
    
    b.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
            //try{
                UserFahrenheit=Med.getText().toString();
    
                //String mobile=getData(UserMRN.trim());
                 String NAMESPACE = "http://tempuri.org/";
                    String METHOD_NAME = "FahrenheitToCelsius";
                    String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
                    String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
    
                    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    
                    Request.addProperty("Fahrenheit",UserFahrenheit.trim());
    
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(Request);
    
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
        try{
                    androidHttpTransport.call(SOAP_ACTION, envelope);
    
                    SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
                    String Celsius;
                    Celsius= String.valueOf(response.toString());
                   Test.setText(Celsius);
    
        }catch(Exception e){
            e.getMessage();
        }
    
    
    
        //  }catch(Exception e){
        //      e.getMessage();
        //  }
    
    
        }
    });
    
        }
    
    
    
    @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;
    }
    
    }
    
4

1 回答 1

1

你为什么使用 SoapPrimitive?首先,检查您的响应是否为错误。否则,通过调用 response.bodyIn 从响应对象中获取响应体:

SoapObject response = null;
if (envelope.bodyIn instanceof SoapObject) { // SoapObject = SUCCESS
    response = (SoapObject) envelope.bodyIn;
} else if (envelope.bodyIn instanceof SoapFault) { // SoapFault =
                                                    // FAILURE
    SoapFault soapFault = (SoapFault) envelope.bodyIn;
    throw new Exception(soapFault.getMessage());
}

然后,您可以通过调用 response.getProperty("responsecode") 从响应中获取属性。

此外,在调试时将 HttpTransportSE 的调试值设置为 true:

transportSE.debug = true;
于 2013-06-15T11:34:36.843 回答