首页 \ 问答 \ Ksoap2 Nullpointer例外(Ksoap2 Nullpointer Exception)

Ksoap2 Nullpointer例外(Ksoap2 Nullpointer Exception)

我有一个Web服务,它运行在Tomcat 7.0.54上(在我的本地机器上)。 它有两个方法, sayHelloFrom(String from) ,它应返回具有给定用户名的问候语字符串。 和saveFile()方法,它在调用时在我的本地机器上生成一个文件。 此外,我还为此服务提供了“标准”java测试客户端。 此外,我使用一个远程Web服务进行测试。 现在我正在尝试将KSOAP2安卓客户端设置为我的网络服务。 所以。 当我使用“标准”java客户端调用我的web服务时 - 一切正常。 当我使用KSOAP2 android客户端调用远程Web服务时,也没有问题。 但是,当我用android KSOAP2客户端调用我的webservice时,我在envelope.getResponse()操作上得到一个NullPointerException,同时调用webservice本身,生成文件,webservice的stacktrace中没有错误:

这是错误堆栈跟踪:

05-26 17:19:33.900  30066-30340/my.test.ru.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:278)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at activity.my.test.ru.WebService$myAsyncTask.doInBackground(WebService.java:84)
            at activity.my.test.ru.WebService$myAsyncTask.doInBackground(WebService.java:38)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

这是我调用webservice的android活动:

public class WebService extends Activity {
    private final String NAMESPACE = "http://example";
    private final String URL = "http://192.168.1.88:8080/services/HelloWorld?wsdl";
    private final String SOAP_ACTION = "http://example/saveFile";
    private final String METHOD_NAME = "saveFile";
    Button b;
    private TextView tv;
    private String response;

    private class myAsyncTask extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            tv.setText(response);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //request.addProperty("from", "Naseko");
            //request.addProperty("iTopN", "5");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(URL);

            httpTransport.debug = true;
            try {
                httpTransport.call(SOAP_ACTION, envelope);
            } catch (HttpResponseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } //send request
            SoapObject result = null;
            try {
                result = (SoapObject) envelope.getResponse();
                Log.d("App", "" + result);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Log.d("App", "" + result.getProperty(1).toString());
            response = result.getProperty(1).toString();
            return null;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_service);
        //Fahrenheit Text control
        tv = (TextView) findViewById(R.id.tv_result);
        //Button to trigger web service invocation
        b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myAsyncTask myRequest = new myAsyncTask();
                myRequest.execute();
            }
        });
    }
}

从清单中得出的结论:

   <uses-permission android:name="android.permission.GET_ACCOUNTS"/> 
   <uses-permission android:name="android.permission.READ_PROFILE"/> 
   <uses-permission android:name="android.permission.READ_CONTACTS"/> 
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

这是我的WSDL:

<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://example" xmlns:intf="http://example" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example">
<!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://example">
<element name="saveFileReturn" type="xsd:string"/>
<element name="from" type="xsd:string"/>
<element name="sayHelloWorldFromReturn" type="xsd:string"/>
</schema>
</wsdl:types>
<wsdl:message name="saveFileRequest"></wsdl:message>
<wsdl:message name="saveFileResponse">
<wsdl:part element="impl:saveFileReturn" name="saveFileReturn"/>
</wsdl:message>
<wsdl:message name="sayHelloWorldFromResponse">
<wsdl:part element="impl:sayHelloWorldFromReturn" name="sayHelloWorldFromReturn"/>
</wsdl:message>
<wsdl:message name="sayHelloWorldFromRequest">
<wsdl:part element="impl:from" name="from"/>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="saveFile">
<wsdl:input message="impl:saveFileRequest" name="saveFileRequest"/>
<wsdl:output message="impl:saveFileResponse" name="saveFileResponse"/>
</wsdl:operation>
<wsdl:operation name="sayHelloWorldFrom" parameterOrder="from">
<wsdl:input message="impl:sayHelloWorldFromRequest" name="sayHelloWorldFromRequest"/>
<wsdl:output message="impl:sayHelloWorldFromResponse" name="sayHelloWorldFromResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="saveFile">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="saveFileRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="saveFileResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHelloWorldFrom">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloWorldFromRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloWorldFromResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldService">
<wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld">
<wsdlsoap:address location="http://192.168.1.88:8080/services/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

那是我的网络服务代码:

@WebService
public class HelloWorld {
    @WebMethod
    public String sayHelloWorldFrom(String from) {
        String result = "Hello, world, from " + from;
        System.out.println(result);
        return result;
    }

    @WebMethod
    public String saveFile() {
        try {
            String content = "This is the content to write into file";
            File file = new File("C:\\test\\filename.txt");
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            System.out.println("Done!!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "OK";
    }
}

I have a web-service, which runs on Tomcat 7.0.54 (on my local machine). It has two methods, sayHelloFrom(String from), which should return greeting string with a given user name. And saveFile() method, which generates a file on my local machine when called. Also I have "standard" java test client for this service. Also, I use this one remote web-service for tests. Now I'm trying to make KSOAP2 android client to my web-service. So. When I call my webservice, using "standard" java client - everything works fine. When I call remote web-service with KSOAP2 android client there is no problems too. But, when I call my webservice with an android KSOAP2 client I get a NullPointerException on envelope.getResponse() action, and at the same time, webservice, itself, is called, file is generated and there is no errors in stacktrace of the webservice:

Here is error stacktrace:

05-26 17:19:33.900  30066-30340/my.test.ru.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:278)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at activity.my.test.ru.WebService$myAsyncTask.doInBackground(WebService.java:84)
            at activity.my.test.ru.WebService$myAsyncTask.doInBackground(WebService.java:38)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

Here is my android activity for calling a webservice:

public class WebService extends Activity {
    private final String NAMESPACE = "http://example";
    private final String URL = "http://192.168.1.88:8080/services/HelloWorld?wsdl";
    private final String SOAP_ACTION = "http://example/saveFile";
    private final String METHOD_NAME = "saveFile";
    Button b;
    private TextView tv;
    private String response;

    private class myAsyncTask extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            tv.setText(response);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            //request.addProperty("from", "Naseko");
            //request.addProperty("iTopN", "5");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(URL);

            httpTransport.debug = true;
            try {
                httpTransport.call(SOAP_ACTION, envelope);
            } catch (HttpResponseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } //send request
            SoapObject result = null;
            try {
                result = (SoapObject) envelope.getResponse();
                Log.d("App", "" + result);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Log.d("App", "" + result.getProperty(1).toString());
            response = result.getProperty(1).toString();
            return null;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_service);
        //Fahrenheit Text control
        tv = (TextView) findViewById(R.id.tv_result);
        //Button to trigger web service invocation
        b = (Button) findViewById(R.id.button1);
        //Button Click Listener
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myAsyncTask myRequest = new myAsyncTask();
                myRequest.execute();
            }
        });
    }
}

And permition from manifest:

   <uses-permission android:name="android.permission.GET_ACCOUNTS"/> 
   <uses-permission android:name="android.permission.READ_PROFILE"/> 
   <uses-permission android:name="android.permission.READ_CONTACTS"/> 
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Here is my WSDL:

<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://example" xmlns:intf="http://example" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example">
<!--
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
-->
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://example">
<element name="saveFileReturn" type="xsd:string"/>
<element name="from" type="xsd:string"/>
<element name="sayHelloWorldFromReturn" type="xsd:string"/>
</schema>
</wsdl:types>
<wsdl:message name="saveFileRequest"></wsdl:message>
<wsdl:message name="saveFileResponse">
<wsdl:part element="impl:saveFileReturn" name="saveFileReturn"/>
</wsdl:message>
<wsdl:message name="sayHelloWorldFromResponse">
<wsdl:part element="impl:sayHelloWorldFromReturn" name="sayHelloWorldFromReturn"/>
</wsdl:message>
<wsdl:message name="sayHelloWorldFromRequest">
<wsdl:part element="impl:from" name="from"/>
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="saveFile">
<wsdl:input message="impl:saveFileRequest" name="saveFileRequest"/>
<wsdl:output message="impl:saveFileResponse" name="saveFileResponse"/>
</wsdl:operation>
<wsdl:operation name="sayHelloWorldFrom" parameterOrder="from">
<wsdl:input message="impl:sayHelloWorldFromRequest" name="sayHelloWorldFromRequest"/>
<wsdl:output message="impl:sayHelloWorldFromResponse" name="sayHelloWorldFromResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSoapBinding" type="impl:HelloWorld">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="saveFile">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="saveFileRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="saveFileResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHelloWorldFrom">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloWorldFromRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloWorldFromResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldService">
<wsdl:port binding="impl:HelloWorldSoapBinding" name="HelloWorld">
<wsdlsoap:address location="http://192.168.1.88:8080/services/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

That is my web-service code:

@WebService
public class HelloWorld {
    @WebMethod
    public String sayHelloWorldFrom(String from) {
        String result = "Hello, world, from " + from;
        System.out.println(result);
        return result;
    }

    @WebMethod
    public String saveFile() {
        try {
            String content = "This is the content to write into file";
            File file = new File("C:\\test\\filename.txt");
            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            System.out.println("Done!!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "OK";
    }
}

原文:https://stackoverflow.com/questions/30460525
更新时间:2023-12-22 08:12

最满意答案

代替 :

toolbar: " undo redo | removeformat | styleselect | bold italic | link | print preview media fullpage", 

尝试(没有styleselect ):

toolbar: " undo redo | removeformat | bold italic | link | print preview media fullpage", 

Instead of :

toolbar: " undo redo | removeformat | styleselect | bold italic | link | print preview media fullpage", 

try (without styleselect):

toolbar: " undo redo | removeformat | bold italic | link | print preview media fullpage", 

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。