0

这是我的代码,我遇到了异常,我无法解决请帮助我,我正在尝试使用 webservice 做 android 应用程序。使用 gateOut 方法计算今天有多少船门

package com.example.marport;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LimanBilgileri extends Activity
{
    /** Called when the activity is first created. */


    final static String NAMESPACE = "http://webservice.marport.dcat.com/";
    // Web servis üzerinde hangi metodu kullanacağımızı ayarlıyoruz.
    final static String METHOD_NAME = "gateOut";
    // SOAP_ACTION : NAMESPACE ve METHOD_NAME birleşiminden oluşuyor.
    final static String SOAP_ACTION = "http://webservice.marport.dcat.com/gateOut";
    // Son olarak Web servise ait URL tanımlamasını burada yapıyoruz.
    final static String URL = "192.168.1.164:8070/test/services/MarportWebServicePort";

    private TextView sonuc;
    private EditText deger;
    private Button hesapla;
    private TextView debug;


    public void onCreate(Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.limanbilgileri);
       debug=(TextView)findViewById(R.id.textView3);

       deneme den=new deneme();
       den.execute();






}

    public class deneme extends AsyncTask<String, String, String>{
        private final ProgressDialog dialog = new ProgressDialog(LimanBilgileri.this);

        String result=null; 
        protected void onPostExecute(String unused) {

          this.dialog.dismiss();
            debug.setText("sonuc " +unused);
       }
        protected void onPreExecute() {
            this.dialog.setMessage("Yükleniyor...");
            this.dialog.show();


          }
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub

            SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);
              //Request.addProperty();
              SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              soapEnvelope.dotNet=false;
              soapEnvelope.setOutputSoapObject(Request);
              HttpTransportSE aht=new HttpTransportSE(URL);
              try {
                aht.call(SOAP_ACTION, soapEnvelope);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              try {
                 result= soapEnvelope.getResponse().toString();

            } catch (SoapFault e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return result;
        }

    }
}

这里有例外

08-16 12:43:42.867: E/AndroidRuntime(2892): FATAL EXCEPTION: AsyncTask #1
08-16 12:43:42.867: E/AndroidRuntime(2892): java.lang.RuntimeException: An error occured while executing doInBackground()
08-16 12:43:42.867: E/AndroidRuntime(2892):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.lang.Thread.run(Thread.java:856)
08-16 12:43:42.867: E/AndroidRuntime(2892): Caused by: java.lang.NullPointerException
08-16 12:43:42.867: E/AndroidRuntime(2892):     at org.ksoap2.serialization.SoapSerializationEnvelope.getResponse(SoapSerializationEnvelope.java:552)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at com.example.marport.LimanBilgileri$deneme.doInBackground(LimanBilgileri.java:90)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at com.example.marport.LimanBilgileri$deneme.doInBackground(LimanBilgileri.java:1)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-16 12:43:42.867: E/AndroidRuntime(2892):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-16 12:43:42.867: E/AndroidRuntime(2892):     ... 5 more
4

3 回答 3

0

异常在这里生成:

result= soapEnvelope.getResponse().toString();

可能你在那条线之前做错了什么。例如,soapEnvelope 可能没有正确初始化。还是必须先执行请求?

更确切地说,错误在SoapSerializationEnvelope.java:552中(由上面的这个调用执行)。但由于您没有发布此文件,我们无法检查。

- - 编辑 - -

我在这里找到了这个文件的一个版本:http ://code.google.com/p/salesforceandroid/source/browse/trunk/SalesforceAndroid/src/org/ksoap2/serialization/SoapSerializationEnvelope.java?r=93 ;似乎是该文件的不同版本(因为没有行号 552);但是有一个方法getResonse():

public Object getResponse() throws SoapFault {
    if (bodyIn instanceof SoapFault) {
        throw (SoapFault) bodyIn;
    }
    KvmSerializable ks = (KvmSerializable) bodyIn;
    return ks.getPropertyCount() == 0 ? null : ks.getProperty(0);
}

这应该产生空指针异常的唯一原因是“bodyIn”(内部变量)为空。此变量在 parseBody() 方法或该对象的 parseXml() 中初始化。所以必须先调用这个,才能成功执行getResponse();

或者:如果这部分由您维护......变量“bodyIn”必须在您的对象生命周期的其他地方初始化。

于 2012-08-23T09:14:26.127 回答
0

听起来您的服务不会从您的请求中返回任何内容。也许您应该确保您的 SOAP 请求是正确的。

于 2012-08-23T09:09:14.583 回答
-1

也许这有帮助,对我来说工作:

if (android.os.Build.VERSION.SDK_INT > 9) 
{
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Android http 连接异常

于 2014-05-13T12:17:39.180 回答