【问题标题】:Android Consume WebService, Exception HttpTransportSE.call()Android 使用 WebService,异常 HttpTransportSE.call()
【发布时间】:2014-10-07 00:31:50
【问题描述】:

晚上好,我很长一段时间都在尝试使用一个 simpes WS,已经研究并尝试了不同的方法来解决这个错误并且失败了,尝试了不同版本的 kSOAP,2.5.2、2.6.0、3.0.0, 3.3,0。 ..Gostaria 喜欢知道如何解决。每次来“transporte.call(SOAP_ACTION sobre);”生成一个空异常。 PS:WS是免费的,如果他们想访问,这是链接: http://www.w3schools.com/webservices/tempconvert.asmx

这是我的代码:

private static final String SOAP_ACTION = "http://tempuri.org/CelsiusT  oFahrenheit";
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String NAMESPACE = "http://tempuri.org";
private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv  = (TextView)findViewById(R.id.textView1);

    try{

        SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
        request.addProperty("Celsius", "32");           

        SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        sobre.dotNet = true;
        sobre.setOutputSoapObject(request);

        HttpTransportSE transporte = new HttpTransportSE(URL, 30000);

        transporte.call(SOAP_ACTION, sobre);

        SoapPrimitive resultado = (SoapPrimitive)sobre.getResponse();

        tv.setText("" + resultado.toString());

    }catch(Exception e){
        tv.setText(e.getMessage());
    }

}

感谢分享知识!

【问题讨论】:

    标签: java android web-services ksoap


    【解决方案1】:

    你得到了错误的命名空间、方法和肥皂动作

    private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
    

    希望对你有帮助

    编辑:

    这行得通:

    private static final String SOAP_ACTION = "http://www.w3schools.com/webservices/CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    //private static final String NAMESPACE = "http://www.w3schools.com/webservices";
    private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
    //private static final String NAMESPACE = "http://www.w3schools.com/webservices/";
    private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
    //private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx?WSDL";
    String result = ""; 
    
    TextView tv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        tv  = (TextView)findViewById(R.id.textView1);
    
        try{
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("Celsius", "32");           
    
            final SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            sobre.dotNet = true;
            sobre.setOutputSoapObject(request);
    
            final HttpTransportSE transporte = new HttpTransportSE(URL, 30000);
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        transporte.call(SOAP_ACTION, sobre);
    
                        SoapPrimitive resultado = (SoapPrimitive)sobre.getResponse();
    
                        result = resultado.toString();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
    
            int countWait = 5; 
            while (result.isEmpty() && countWait > 0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
                countWait--;
            }
            tv.setText(result);
    
        }catch(Exception e){
            tv.setText(e.getMessage());
        }
    
    }
    

    问题是您无法在主线程中进行网络操作。加上一些命名空间和其他常量问题。

    【讨论】:

    • 你好Serge,我改变了“命名空间,方法,soap动作”,没有解决,仍然是方法“.call()”中的异常
    • 我查看了 WSDL,它实际上显示了private static final String NAMESPACE = "http://www.w3schools.com/webservices/";,末尾带有斜线。你可以试试这个吗?
    • 你能检查一下 transporte 是否为 null 吗?您还可以在问题中添加堆栈吗?
    • 是的,传输返回空异常,你有电子邮件吗?如果您愿意,我可以通过您的电子邮件发送项目
    【解决方案2】:

    来自 serge 的代码可以工作,但需要一些东西

    所以看不到线程,需要警察在主线程中访问添加这个

    `StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy);

    或使用 asinctask

    class DemoTask extends AsyncTask<Void, Void, Void> {
    
    protected Void doInBackground(Void... arg0) {
        //Your implementation 
    }
    
    protected void onPostExecute(Void result) {
        // TODO: do something with the feed
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多