尝试call()时出现kSoap null错误

时间:2016-08-25 11:02:44

标签: java android web-services ksoap2

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

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

该方法本身并没有什么特别之处,取任何参数,只返回“Hello World”。我尝试更改它,以便它确实需要一个字符串并返回它,但这也不起作用。这是我第一次处理这些事情。 我必须在发布之前更改网址,因为它是私人公司信息,网址工作正常,我很确定。

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$3$override.onClick(Podesavanja.java:153)
   at com.example.nfc.Podesavanja$3$override.access$dispatch(Podesavanja.java)
   at com.example.nfc.Podesavanja$3.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)

1 个答案:

答案 0 :(得分:0)

我解决了问题 - 添加了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();
            }

        }


    });

然后创建了这个类:

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");
    }

}