尝试调用时出现 kSoap 空错误()

kSoap null error when attempting call()

我在尝试访问 Web 服务中的方法时遇到问题,一切正常,直到到达 transport.call() 行。 try/catch 块失败,我得到输出:

08-25 12:38:45.380 28998-28998/com.example.nfc E/...: 错误:空

该方法本身没有做任何特别的事情,接受任何参数,只是returns "Hello World"。我试着改变它,让它接受一个字符串并 returns 它,但这也不起作用。这是我第一次和这些孩子打交道。 我必须在发帖之前更改 url 地址,因为它是私人公司信息,URL 工作正常,我确信这一点。

String NAMESPACE = "http://tempuri.org/";
String METHOD_NAME = "HelloWorld";
String SOAP_ACTION = "http://tempuri.org/HelloWorld";
String URL= "http://example.com/example.asmx";
    protected void onCreate(Bundle savedInstanceState) {
    .........

    btnOdustani.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                soapEnvelope.dotNet = true;
                soapEnvelope.setOutputSoapObject(Request);

                HttpTransportSE transport = new HttpTransportSE(URL, 10000);
            try
            {

                transport.call(SOAP_ACTION,soapEnvelope);
                SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse();

                String result = response.toString();

            Toast.makeText(Podesavanja.this,result,Toast.LENGTH_LONG).show();
                Log.i(TAG, "Result: " + result);
            } catch (Exception ex) {
                Log.e(TAG, "Error: " + ex.getMessage());
    Log.d("Error", Log.getStackTraceString(ex));
            }

        }
    });

完整堆栈跟踪:

     08-25 13:24:55.109 30285-30285/com.example..nfc E/Error: java.net.SocketTimeoutException
   at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:130)
   at com.example..nfc.Podesavanja$override.onClick(Podesavanja.java:153)
   at com.example.nfc.Podesavanja$override.access$dispatch(Podesavanja.java)
   at com.example.nfc.Podesavanja.onClick(Podesavanja.java:0)
   at android.view.View.performClick(View.java:4832)
   at android.view.View$PerformClick.run(View.java:19844)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5319)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

我解决了这个问题 - 添加了一个 AsyncTask。 将此代码添加到 btnOdustani.OnClickListener

btnOdustani.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                 AsyncCallWS task = new AsyncCallWS();
            try {
               String pera= task.execute().get();
                Toast.makeText(Podesavanja.this,pera,Toast.LENGTH_LONG).show();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }


    });

然后创建这个 class:

private class AsyncCallWS extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
    }
    @Override
    protected String doInBackground(Void... params) {
        Log.i(TAG, "doInBackground");

            SoapPrimitive response = null;
                try {
                    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    soapEnvelope.dotNet = true;
                    soapEnvelope.setOutputSoapObject(Request);
                    HttpTransportSE transport = new HttpTransportSE(URL);
                    transport.call(SOAP_ACTION, soapEnvelope);
                    response = (SoapPrimitive) soapEnvelope.getResponse();
                    result1= response.toString();
                } catch (Exception ex) {
                    Log.e(TAG, "Error: " + ex.getMessage());
                }
        return response.toString();
    }
    @Override
    protected void onPostExecute(String result) {
        Log.i(TAG, "onPostExecute");
    }

}