Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ksoap2空指针异常_Java_Android_Web Services_Wsdl_Ksoap2 - Fatal编程技术网

Java Ksoap2空指针异常

Java Ksoap2空指针异常,java,android,web-services,wsdl,ksoap2,Java,Android,Web Services,Wsdl,Ksoap2,我有一个web服务,它在本地机器上的Tomcat7.0.54上运行。它有两个方法,sayHelloFromString from,它应该返回带有给定用户名的问候语字符串。和saveFile方法,该方法在调用时在本地计算机上生成一个文件。 此外,我还有用于此服务的标准java测试客户端。 此外,我使用一个远程web服务进行测试。 现在我正在尝试将KSOAP2 android客户端添加到我的web服务中。 所以当我使用标准java客户机调用我的Web服务时,一切正常。 当我使用KSOAP2 andr

我有一个web服务,它在本地机器上的Tomcat7.0.54上运行。它有两个方法,sayHelloFromString from,它应该返回带有给定用户名的问候语字符串。和saveFile方法,该方法在调用时在本地计算机上生成一个文件。 此外,我还有用于此服务的标准java测试客户端。 此外,我使用一个远程web服务进行测试。 现在我正在尝试将KSOAP2 android客户端添加到我的web服务中。 所以当我使用标准java客户机调用我的Web服务时,一切正常。 当我使用KSOAP2 android客户端调用远程web服务时,也没有问题。 但是,当我使用android KSOAP2客户端调用我的Web服务时,我在envelope.getResponse操作上得到一个NullPointerException,同时调用Web服务本身,生成文件,并且Web服务的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)
以下是我调用Web服务的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();
            }
        });
    }
}
明白了

首先,我把doInBackground里面的所有东西都放到了try块中。 然后我得到了新的ecxeption,它是classCastException。 因此,我将获得结果的代码行更改为:

            SoapPrimitive result =(SoapPrimitive)envelope.bodyIn;
            Log.d("App", "" + result.toString());
            response = result.toString();
现在,hole doInBackground方法如下所示:

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(URL);
            //httpTransport.debug = true;
            httpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive result =(SoapPrimitive)envelope.bodyIn;
            Log.d("App", "" + result.toString());
            response = result.toString();
        } catch (IOException | XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  //send request
        return null;
    }
}
@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";
    }
}
            SoapPrimitive result =(SoapPrimitive)envelope.bodyIn;
            Log.d("App", "" + result.toString());
            response = result.toString();
    @Override
    protected Void doInBackground(Void... arg0) {
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(URL);
            //httpTransport.debug = true;
            httpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive result =(SoapPrimitive)envelope.bodyIn;
            Log.d("App", "" + result.toString());
            response = result.toString();
        } catch (IOException | XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  //send request
        return null;
    }
}