0

當我嘗試從以下一個Android應用程序調用 web服務我得到下面的錯誤是我的logcat安卓:Webservice的拋出異常

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: [email protected] 
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; 
    } 
} 
+0

你能發佈你的代碼嗎? –

+0

發佈您的代碼。我認爲問題在於傳遞參數。 – Riser

+0

我已經發布代碼以及... –

回答

0

看來發送方法有錯誤,我看不到哪個變量沒有初始化。但是有很多雜亂的東西。我認爲這兩種方法應該只用這個代碼:

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

你是否確定你的命名空間,動作,url和方法是正確的?

+0

與您的代碼,當我嘗試它在locahost上它完美,但不是在主要網絡服務器上。它會拋出同樣的例外 –

+0

只有兩個問題浮現在腦海。 1 - 您的web.config文件僅用於本地交互。 2 - 您的在線服務器具有舊版本的服務。 –

+0

該webservice是一個網站的一部分。該網站正在工作。 www.enoxonline.com,甚至webservice的路徑都是正確的。有沒有可能當我傳遞的字符串到達​​web服務時可以爲空。或者在webservice中有任何問題。 –