将参数从android传递到.Net soap webservice

时间:2011-11-11 09:57:32

标签: android web-services ksoap

我将lat的参数传递给我的android应用程序的.net soap webservice,因为我使用了以下代码,但它没有给我任何错误,也没有给我任何输出。

package pkg.CurrentLocation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class CurrentLocationActivity extends Activity {
    /** Called when the activity is first created. */
    String imei;
    String number;
    String Lat;
    String Lng;
    TextView txt;

    private static final String NAMESPACE = "http://localhost/AndroidGeoWebService";
    // private static final String URL =
    // "http://172.16.8.135/AndroidWebService/AndroidSQL.asmx";
    private static final String URL = 
    "http://172.16.8.140/AndroidGeoWebService/WebService.asmx";

    private static final String SAGeoLocation = 
      "http://localhost/AndroidGeoWebService/GeoLocation";
    private static final String MNGeoLocation = "GeoLocation";
    ArrayList<String> listDistace = new ArrayList<String>();
    ArrayList<String> listdecDrLatitude = new ArrayList<String>();
    ArrayList<String> listdecDrLongitude = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txt = (TextView) findViewById(R.id.txtview);
        TelephonyManager manager = 
         (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        imei = manager.getDeviceId();
        String number = manager.getLine1Number();

        CalculateDistance(Lat, Lng);

        // Toast.makeText(CurrentLocationActivity.this, " IMEI Number: "+imei,
        // Toast.LENGTH_LONG).show();

        LocationManager lm = 
             (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        /*
         * if(LocationManager.GPS_PROVIDER.equals("gps")){
         * lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, ll);
         * }else
         */// if(LocationManager.NETWORK_PROVIDER.equals("network")){
        // lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0,
        // ll);
        // }
        // else if(LocationManager.GPS_PROVIDER.equals("gps")){
        // lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, ll);
        // }else{
        // Toast.makeText(CurrentLocationActivity.this,
        // "Neither GPS nor Netwok Provider Found", Toast.LENGTH_LONG).show();
        // }
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0, ll);

    }

    private class mylocationlistener implements LocationListener {

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                Log.d("LOCATION CHANGED", location.getLatitude() + "");
                Log.d("LOCATION CHANGED", location.getLongitude() + "");
                Toast.makeText(
        CurrentLocationActivity.this,
    location.getLatitude() + " " + location.getLongitude()
            + " IMEI Number: " + imei + " Mobile Number:"
                                + number, 30).show();
                Lat = String.valueOf(location.getLatitude());
                Lng = String.valueOf(location.getLongitude());
                SyncGeo();

                CalculateDistance(Lat, Lng);
                Log.d("Called", "Calculate distance");
    final ArrayList<Map<String, String>> data = new ArrayList<Map<String, String>>();
                Toast.makeText(CurrentLocationActivity.this,
                        "" + listDistace.size(), 30).show();
                for (int i = 0; i < listDistace.size(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();
    map.put("DrLatitude",                            String.valueOf(listdecDrLatitude.get(i)));
    map.put("DrLongitude", listdecDrLongitude.get(i));
                    data.add(map);


                }

            }
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

    }

    void SyncGeo() {

        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                URL);

        // XMLfunctions //XMLfunctions=new XMLfunctions();

        try {
            // XMLfunctions=new XMLfunctions();
            SoapObject requestGeoLocation = new SoapObject(NAMESPACE,
                    MNGeoLocation);

            requestGeoLocation.addProperty("varMobile", "1111122222");
            requestGeoLocation.addProperty("varLatitute", Lat);
            requestGeoLocation.addProperty("varLongitute", Lng);
            requestGeoLocation.addProperty("varIMEI", imei);

        SoapSerializationEnvelope envelopeDrGifts = 
            new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelopeDrGifts.dotNet = true;
            envelopeDrGifts.setOutputSoapObject(requestGeoLocation);
            androidHttpTransport.call(SAGeoLocation, envelopeDrGifts);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // Method for callculating Distance

    public String CalculateDistance(String Lat, String Lng) {
        final String NAMESPACE = "http://localhost/My_WebService/";

        final String URL = "http://172.16.8.140/My_WebService/My_WebService.asmx";

        final String SAEmployee = "http://localhost/My_WebService/Employee_Mst";
        final String MNEmployee = "Employee_Mst";



        AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
                URL);
        XMLfunctions XMLfunctions = new XMLfunctions();
        // HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {

    SoapObject requestDoctorMst = new SoapObject(NAMESPACE, MNEmployee);
            requestDoctorMst.addProperty("decLatitude", "22.00");
            requestDoctorMst.addProperty("decLongitude", "73.00");

    SoapSerializationEnvelope envelopeDoctorMst = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
             envelopeDoctorMst.dotNet = true;
            envelopeDoctorMst.setOutputSoapObject(requestDoctorMst);
            androidHttpTransport.call(SAEmployee, envelopeDoctorMst);

    SoapObject resultsRequestSOAPDoctorMst = (SoapObject) envelopeDoctorMst.bodyIn;
            String xmEmployeeMst = resultsRequestSOAPDoctorMst.toString();
            Log.d("xmEmployeeMst", "" + xmEmployeeMst);

            if (xmEmployeeMst.contains("Error:")) {
            } else {
                if (xmEmployeeMst.contains("Table")) {
                    Document docDoctorMst = XMLfunctions
                            .XMLfromString(xmEmployeeMst);
                    if (!docDoctorMst.equals(null)) {
     NodeList nodesDoctorMst = 
                docDoctorMst.getElementsByTagName("Table");
        Log.d("Before for", "Before for");
    Log.d("nodesDoctorMST", "" + nodesDoctorMst.getLength());
        for (int i = 0; i < nodesDoctorMst.getLength(); i++) {
        Element calculateDist = (Element) nodesDoctorMst.item(i);
                listDistace.add(XMLfunctions.getValue(
                calculateDist, "Distance"));
        listdecDrLatitude.add(XMLfunctions.getValue(
            calculateDist, "decDrLatitude"));
    listdecDrLongitude.add(XMLfunctions.getValue(
        calculateDist, "decDrLongitude"));
    Log.d("inside for","insdie for"+XMLfunctions.getValue(                  calculateDist,"decDrLatitude"));
 //          Toast.makeText(CurrentLocationActivity.this,
 // XMLfunctions.getValue(calculateDist,
// "decDrLatitude"), 300).show();

                        }
                        Log.d("After for", "After for");
                    } else {

                    }

                }

            }

        } catch (org.xmlpull.v1.XmlPullParserException ex2) {
            String msg2 = ex2.toString();
            String request = androidHttpTransport.requestDump;
            String response = androidHttpTransport.responseDump;

        } catch (Exception e) {
            e.printStackTrace();

        }

        Toast.makeText(CurrentLocationActivity.this,
                listdecDrLatitude.toString(), 30).show();
        return null;

    }

}

我的Web服务代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

using System.Data;
using System.Configuration;
using System.Text;
using System.IO;
using System.Xml;
using System.Security.Cryptography;


/// <summary>
/// Summary description for My_WebService
/// </summary>
[WebService(Namespace = "http://localhost/My_WebService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class My_WebService : System.Web.Services.WebService {
    String strResonse;
    public My_WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public string Employee_Mst(string decLatitute, string decLongitute)
    {

        SqlConnection myConn = new SqlConnection();

       // myConn.ConnectionString = "Data Source=IBMVCS1;Initial Catalog=TestSales_DSS;User ID=sa;Password=vcs@123";
        myConn.ConnectionString = "Data Source=IBMVCS1;Initial Catalog=DemoLocation;User ID=sa;Password=vcs@123";
       // myConn.Open();
        //"+varEmpName+"
       string strqry = "SELECT dbo.fn_GreatCircleDistance('"+ decLatitute +"','"+ decLongitute +"',dl.decDrLatitude,dl.decDrLongitude) AS Distance, dl.decDrLatitude,dl.decDrLongitude FROM dbo.CurrentLocation AS cl, dbo.DrLocation dl WHERE dbo.fn_GreatCircleDistance('" + decLatitute + "','" + decLongitute + "',dl.decDrLatitude,dl.decDrLongitude) <=100";

        //string strqry = "SELECT dbo.fn_GreatCircleDistance('22.00','73.00',dl.decDrLatitude,dl.decDrLongitude) AS Distance, dl.decDrLatitude,dl.decDrLongitude FROM dbo.CurrentLocation AS cl, dbo.DrLocation dl WHERE dbo.fn_GreatCircleDistance('22.00','73.00',dl.decDrLatitude,dl.decDrLongitude) <=100";

       // string strqry = "SELECT dbo.fn_GreatCircleDistance('" + decLatitute + "','" + decLongitute + "',dl.decDrLatitude,dl.decDrLongitude) AS Distance, dl.decDrLatitude,dl.decDrLongitude FROM dbo.CurrentLocation AS cl, dbo.DrLocation dl WHERE dbo.fn_GreatCircleDistance('" + decLatitute + "','" + decLongitute + "',dl.decDrLatitude,dl.decDrLongitude) <=100";
        DataSet ds = new DataSet();
        strResonse = "";
       // SqlCommand myCom = new SqlCommand(strqry, myConn);
       // int numrow = myCom.ExecuteNonQuery();

        try
        {
            myConn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strqry.ToString(), myConn);
            da.Fill(ds);

            strResonse = ds.GetXml();
            // return ds.GetXml();
        }
        catch (SqlException ex)
        {
            //return null;
            strResonse = "Error:" + ex.ToString();

        }
        catch (Exception ex)
        {
            // return null;
            strResonse = "Error:" + ex.ToString();
        }
        finally
        {
            if (myConn.State == System.Data.ConnectionState.Open)
            {
                myConn.Close();
            }
        }
        return strResonse;
    }

}

0 个答案:

没有答案