Android:Webservice抛出异常

时间:2013-05-15 07:14:55

标签: android web-services nullpointerexception android-ksoap2

当我尝试从Android应用程序调用webservice时,我得到以下错误 以下是我的logcat

05-15 12:33:26.327: E/Error :(339): Error on IOException() System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Object reference not set to an instance of an object.
05-15 12:33:26.327: E/Error :(339):    at WebService.SetLocationForAndroid(String Latitude, String Longitude, String sUserId)
05-15 12:33:26.327: E/Error :(339):    --- End of inner exception stack trace ---
05-15 12:33:26.327: W/System.err(339): SoapFault - faultcode: 'soap:Server' faultstring: 'System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Object reference not set to an instance of an object.
05-15 12:33:26.337: W/System.err(339):    at WebService.SetLocationForAndroid(String Latitude, String Longitude, String sUserId)
05-15 12:33:26.337: W/System.err(339):    --- End of inner exception stack trace ---' faultactor: 'null' detail: org.kxml2.kdom.Node@43e9fac0
05-15 12:33:26.377: W/System.err(339):  at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:141)
05-15 12:33:26.377: W/System.err(339):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
05-15 12:33:26.387: W/System.err(339):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
05-15 12:33:26.387: W/System.err(339):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:214)
05-15 12:33:26.397: W/System.err(339):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
05-15 12:33:26.397: W/System.err(339):  at com.practice.locationtracking.LocationService.sendLocation(LocationService.java:144)
05-15 12:33:26.397: W/System.err(339):  at com.practice.locationtracking.LocationService.access$0(LocationService.java:114)
05-15 12:33:26.397: W/System.err(339):  at com.practice.locationtracking.LocationService$1.run(LocationService.java:101)
05-15 12:33:26.397: W/System.err(339):  at android.os.Handler.handleCallback(Handler.java:587)
05-15 12:33:26.407: W/System.err(339):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 12:33:26.407: W/System.err(339):  at android.os.Looper.loop(Looper.java:123)
05-15 12:33:26.417: W/System.err(339):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-15 12:33:26.417: W/System.err(339):  at java.lang.reflect.Method.invokeNative(Native Method)
05-15 12:33:26.417: W/System.err(339):  at java.lang.reflect.Method.invoke(Method.java:521)
05-15 12:33:26.417: W/System.err(339):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-15 12:33:26.417: W/System.err(339):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-15 12:33:26.417: W/System.err(339):  at dalvik.system.NativeStart.main(Native Method)

请帮我解决我的问题。问题在哪里,我找不到..

我的服务的代码如下,它将数据发送到Web服务。

package com.practice.locationtracking;

import java.io.IOException;
import java.net.SocketException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class LocationService extends Service
{

    GPSTracker gps;
    private Handler handler = new Handler();

    String latitude="27.40", longitude="72.40";
    String UserId = "";

    private static final String TAG = "LocationService";
    //used for getting the handler from other class for sending messages
    public static Handler mMyServiceHandler = null;
    //used for keep track on Android running status
    public static Boolean mIsServiceRunning = false;

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate()
    {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        UserId = prefs.getString("user_name", null);
        Toast.makeText(getApplicationContext(), "User Name is : " + UserId, Toast.LENGTH_SHORT).show();

        //Toast.makeText(this, "Location Tracking Service Created",Toast.LENGTH_SHORT).show();
        Log.d(TAG,"On Create");
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        Toast.makeText(this, "Location Tracking Service Started",Toast.LENGTH_SHORT).show();
        Log.d(TAG,"On Start");          
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
    {
        Toast.makeText(this, "Location Tracking Service Started",Toast.LENGTH_SHORT).show();
        //Toast.makeText(getApplicationContext(), "User Name is : " + UserId, Toast.LENGTH_SHORT).show();
        mIsServiceRunning = true;
        handler.post(timedTask);
        return START_STICKY;
    }

    @Override
    public void onDestroy() 
    {
        handler.removeCallbacks(timedTask);
        Toast.makeText(this, "Location Tracking Service Stopped", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onDestroy");

        mIsServiceRunning = false; // make it false, as the service is already destroyed.
    }

    private Runnable timedTask = new Runnable()
    {
       @Override
       public void run()
       {
           gps = new GPSTracker(LocationService.this);
           if(gps.canGetLocation())
           {
              double lat = gps.getLatitude();
              double lon = gps.getLongitude();
              latitude = Double.toString(lat);
              longitude = Double.toString(lon);

              if(latitude.equalsIgnoreCase("0.0") || longitude.equalsIgnoreCase("0.0"))
              {
                  Toast.makeText(getApplicationContext(), "Can not get Location Details from Device", Toast.LENGTH_SHORT).show();
                 // System.out.println("We were here");

              }
              else
              {
                  boolean flag = sendLocation(latitude, longitude, UserId);
                  //boolean flag = true;
                  Toast.makeText(getApplicationContext(), "Your Location is - \nLatitude: " + latitude + "\nLongitude: " + longitude + "\n Flag: " + flag, Toast.LENGTH_SHORT).show();
              }
           }
           else
           {
                    gps.showSettingsAlert();
            }
            handler.postDelayed(timedTask, 5000);
       }
    };

    private boolean sendLocation(String latitude, String longitude, String UserName)
    {
    /*  final String NAMESPACE = "http://tempuri.org/";
        final String URL = "http://10.0.2.2:25722/NewLocationMap/Service.asmx";
        final String SOAP_ACTION = "http://tempuri.org/SetLocationForAndroid";
        final String METHOD_NAME = "SetLocationForAndroid";
    */
        final String NAMESPACE = "http://www.enoxonline.in/";
        final String URL = "http://www.enoxonline.in/Webservice.asmx";
        final String SOAP_ACTION = "http://tempuri.org/SetLocationForAndroid";
        final String METHOD_NAME = "SetLocationForAndroid";

        boolean flag = false;

        SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);   
        PropertyInfo pi = new PropertyInfo();
        pi.setName("Latitude");
        pi.setValue(latitude);
        pi.setType(String.class);
        request.addProperty(pi);
        pi = new PropertyInfo();
        pi.setName("Longitude");
        pi.setValue(longitude);
        pi.setType(String.class);
        request.addProperty(pi);
        pi = new PropertyInfo();
        pi.setName("UserId");
        pi.setValue(UserName);
        pi.setType(String.class);
        request.addProperty(pi);

//      request.addProperty("Latitude",latitude);
//      request.addProperty("Longitude", longitude);
//      request.addProperty("sUserId", UserName);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.xsd = NAMESPACE;
        envelope.enc = "http://schemas.datacontract.org/2004/07/Entity";
        envelope.setOutputSoapObject(request);
        System.out.println(request);

        try
        {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            androidHttpTransport.call(SOAP_ACTION, envelope);

            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            //Log.e("Result: ", "" + response.toString());
            System.out.println(response.toString());
            if(response.toString().equalsIgnoreCase("true"))
            {
                flag=true;
            }
            else
            {
                flag=false;
            }
        }
        catch(SocketException ex)
        {
            Log.e("Error : " , "Error on SocketException " + ex.getMessage());
            ex.printStackTrace();
        }
        catch(IOException ex1)
        {
            Log.e("Error : " , "Error on IOException() " + ex1.getMessage());
            ex1.printStackTrace();
        }
        catch(Exception e)
        {
            Log.e("Error : " , "Error on IOException() " + e.getMessage());
            e.printStackTrace();            
        }
        return flag;
    }
}

1 个答案:

答案 0 :(得分:0)

似乎send方法中存在错误,我无法看到哪个变量未初始化。但是有很多混乱的东西。我认为这两种方法只适用于这段代码:

private Runnable timedTask = new Runnable()
{
   @Override
   public void run()
   {
       gps = new GPSTracker(LocationService.this);
       if(gps.canGetLocation())
       {
          latitude = Double.toString(gps.getLatitude());
          longitude = Double.toString(gps.getLongitude());

          if(latitude.equalsIgnoreCase("0.0") && longitude.equalsIgnoreCase("0.0"))
              Toast.makeText(getApplicationContext(), "Can not get Location Details from Device", Toast.LENGTH_SHORT).show();
          else
          {
              boolean flag = sendLocation(latitude, longitude, UserId);
              Toast.makeText(getApplicationContext(), "Your Location is - \nLatitude: " + latitude + "\nLongitude: " + longitude + "\n Flag: " + flag, Toast.LENGTH_SHORT).show();
          }
       }
       else
       {
           gps.showSettingsAlert();
       }
       handler.postDelayed(timedTask, 5000);
   }
};

private boolean sendLocation(String latitude, String longitude, String UserName)
{
    final String NAMESPACE = "http://www.enoxonline.in/";
    final String URL = "http://www.enoxonline.in/Webservice.asmx";
    final String SOAP_ACTION = "http://tempuri.org/SetLocationForAndroid";
    final String METHOD_NAME = "SetLocationForAndroid";

    try
    {
        SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);
        request.addProperty("Latitude", latitude);
        request.addProperty("Longitude", longitude);
        request.addProperty("UserId", UserName);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);


        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        return Boolean.getBoolean(envelope.getResponse().toString());
    }
    catch(Exception e)
    {
        Log.e(TAG, "Error : " e.toString());
        return false;       
    }
}

您还确定您的命名空间,操作,网址和方法是否正确?