HttpTransport.call(SOAP_ACTION1,信封)不工作?

时间:2014-05-02 04:22:49

标签: android ksoap2

private static String SOAP_ACTION1 = "http://www.w3schools.com/webservices/FahrenheitToCelsius";
private static String SOAP_ACTION2 = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
private static String NAMESPACE = "http://www.w3schools.com/webservices/";
private static String METHOD_NAME1 = "FahrenheitToCelsius";
private static String METHOD_NAME2 = "CelsiusToFahrenheit";
private static String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
Button btnFar,btnCel,btnClear;
EditText txtFar,txtCel;
int value;
SoapObject result;

我的代码如下所示

public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);    
  setContentView(R.layout.activity_main);
  btnFar = (Button)findViewById(R.id.btnFar);
  btnCel = (Button)findViewById(R.id.btnCel);
  btnClear = (Button)findViewById(R.id.btnClear);
  txtFar = (EditText)findViewById(R.id.txtFar);
  txtCel = (EditText)findViewById(R.id.txtCel);
  btnFar.setOnClickListener(new View.OnClickListener()
  {
        @Override
        public void onClick(View v)
        {
            value = Integer.parseInt(txtFar.getText().toString());
            new soapFahrenheit().execute();
        }
  });               

  btnClear.setOnClickListener(new View.OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
          txtCel.setText("");
          txtFar.setText("");
    }
  });
}

private class soapFahrenheit extends AsyncTask<String, Integer, String> {

  protected void onPreExecute()
  {
     //
      txtCel.setText(value);
  }
  @Override
  protected String doInBackground(String... arg0){

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);    

        //Use this to add parameters
        request.addProperty("Fahrenheit",value);         

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);          

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;         

        try {
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);                
              androidHttpTransport.call(SOAP_ACTION1, envelope);
              result = (SoapObject)envelope.bodyIn;
        } catch (Exception e) {
              e.printStackTrace();
        }           
        return result.getProperty(0).toString();
  }

  protected void onPostExecute(SoapObject result)
  {
      if(result != null)
      {
          txtFar.setText(result.getProperty(0).toString());
      }
      else
      {
           Toast.makeText(getApplicationContext(), "No Response",Toast.LENGTH_LONG).show();
      }
  }
 }
}
我在这里错过了什么吗?这是杀了我的日子....请帮忙

2 个答案:

答案 0 :(得分:0)

您正在主UI线程上进行网络通信,而不允许Android 4.0 +。

因此,您需要创建一个后台线程来向服务器发出请求。

AsyncTask 是你应该使用的。

答案 1 :(得分:0)

ACTION字符串的问题

http://www.w3schools.com/webservices/tempconvert.asmx?op=FahrenheitToCelsius

和xml表单

   POST /webservices/tempconvert.asmx HTTP/1.1
Host: www.w3schools.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.w3schools.com/webservices/FahrenheitToCelsius" // ACTION

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <FahrenheitToCelsius xmlns="http://www.w3schools.com/webservices/">
      <Fahrenheit>string</Fahrenheit>
    </FahrenheitToCelsius>
  </soap:Body>
</soap:Envelope>

所以ACTION名称应该是

private static String SOAP_ACTION1 = "http://www.w3schools.com/webservices/FahrenheitToCelsius";

private static String SOAP_ACTION2 = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";

希望这能解决您的问题。

并且在UI线程上使用不做网络操作。使用AsynTask

编辑:

private void doNetwork() {
        SoapObject resultRequestSOAP = new SoapObject(NAMESPACE1, METHOD_NAME1);
        PropertyInfo pi1 = new PropertyInfo();
        pi1.setName("Fahrenheit");
        pi1.setValue(""+value);// Say value= 10
        pi1.setType(PropertyInfo.STRING_CLASS);
        resultRequestSOAP.addProperty(pi1);
        SoapSerializationEnvelope envp = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envp.dotNet = true;
        try {
            envp.setOutputSoapObject(resultRequestSOAP);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.debug = true;
            androidHttpTransport.call(SOAP_ACTION1, envp);
            SoapObject response = (SoapObject) envp.bodyIn;
            System.out.println("response"+response.toString() + "  "+response.getProperty(0).toString());
        } catch (Exception e) {

        }

    }

输出:

 FahrenheitToCelsiusResponse{FahrenheitToCelsiusResult=-12.2222222222222; }