如何在android中使用web服务从sql server获取数据

问题描述 投票:1回答:2

如何在android中使用Web服务从sql server获取数据,并在下一页的listview中显示数据,单击按钮。将android应用程序连接到webservices的方法是什么,我将代码插入到webservices中

这是我的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ab" >

<TextView
    android:id="@+id/text_persons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="19dp"
    android:layout_marginTop="90dp"
    android:text="@string/text_persons"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/text_amount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text_persons"
    android:layout_marginTop="46dp"
    android:layout_toLeftOf="@+id/edit_persons"
    android:text="@string/text_amount"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/edit_persons"
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/text_persons"
    android:layout_marginLeft="14dp"
    android:layout_toRightOf="@+id/text_persons"
    android:ems="10"
    android:hint="@string/edit_persons"
    android:inputType="number" />

<EditText
    android:id="@+id/edit_amount"
    android:layout_width="175dp"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/text_amount"
    android:layout_alignLeft="@+id/edit_persons"
    android:ems="10"
    android:hint="@string/edit_amount"
    android:inputType="number" />

<Button
    android:id="@+id/button_findfood"
    android:layout_width="200dp"
    android:layout_height="45dp"
    android:layout_below="@+id/text_amount"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="54dp"
    android:text="@string/button_findfood" />

<Button
    android:id="@+id/button1"
    android:layout_width="200dp"
    android:layout_height="45dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="64dp"
    android:text="@string/button_map" />

这是我的mainactivity.java`

public class MainActivity extends Activity {

EditText editPersons, editAmount;
String youramount, yourpersons;
//KSOAP
        final String SOAP_ACTION = "http://tempuri.org/Products";
        final String METHOD_NAME = "Products";
        final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
        final String SOAP_ADDRESS = "http://localhost:22781/WebService.asmx?op=Products";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Go to Next Page
    Button button = (Button) findViewById(R.id.button_findfood);
    button.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent i=new Intent(getApplicationContext(), DisplayMessageActivity.class);
            startActivity(i);

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, METHOD_NAME);
    PropertyInfo propertyInfo = new PropertyInfo();
    propertyInfo.name="amount";
    propertyInfo.name="persons";

    editPersons=(EditText)findViewById(R.id.edit_persons);
    editAmount=(EditText)findViewById(R.id.edit_amount);
    yourpersons=editPersons.getText().toString();
    youramount=editAmount.getText().toString();
    request.addProperty(propertyInfo);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

        try  { 
            httpTransport.call(SOAP_ACTION, envelope);                    
            Object response = envelope.getResponse();                    
        }catch (Exception exception)   {

   }            
        }
    });

}

这是我的网络服务

    [WebMethod]
public DataSet Products(decimal amount, decimal persons)
{

    decimal price = amount / persons;
    DataSet result = null;
    const string SQL_COMMAND_TEXT = "SELECT Menu,Price FROM ASD WHERE Price <= @price";
    using (SqlConnection connection = WebSerConnection.GetConnection())
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand(SQL_COMMAND_TEXT, connection))
        {
            command.Parameters.Add("@Persons", SqlDbType.VarChar);
            command.Parameters.Add("@price", SqlDbType.Int);
            command.Parameters["@persons"].Value = persons;
            command.Parameters["@price"].Value = price;
            using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
            {
                result = new DataSet();
                dataAdapter.Fill(result);
            }
        }
    }

    return result;
}
android sql
2个回答
0
投票
  1. 首先,对您来说最重要的是:您无法进行Web API调用,也无法直接在主UI上执行任何长时间运行的任务。仅供参考,您正在使用onCreate()方法直接进行Web调用而不实现线程机制。 您可以实现AsyncTask来解决此问题。
  2. 其次,您不能使用http://localhost,因为它指的是您尝试测试/运行应用程序的设备。检查有关此问题的更多信息:How to connect to my http://localhost web server from Android Emulator in Eclipse

0
投票
public class CallSoap 
{

public String SOAP_ACTION = "http://tempuri.org/CreateEvent";

public String OPERATION_NAME = "CreateEvent"; 

public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

public String erorr="";

public  final String SOAP_ADDRESS = "http://xxxx/Service1.asmx";

SoapObject request;
SoapSerializationEnvelope envelope;

//AndroidHttpTransport androidHttpTransport;
HttpTransportSE androidHttp;

public CallSoap() 
{ 
}


protected void SetEnvelope() {

    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 

        // Creating SOAP envelope           
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        //You can comment that line if your web service is not .NET one.
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);
        androidHttp = new HttpTransportSE("xxxx/Service1.asmx");

        androidHttp.debug = true;

    } catch (Exception e) {
        System.out.println("Soap Exception---->>>" + e.toString());    
    }
}

@SuppressLint("SimpldurationFormat")
public int Send(decimal amount, decimal persons)
{
    int id=0;
    try{
            //request
            request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

            PropertyInfo pi5=new PropertyInfo();
            pi5.setType(decimal.class);
            pi5.setName("amount");
            pi5.setValue(amount);
            request.addProperty(pi5);

            PropertyInfo pi6=new PropertyInfo();
            pi6=new PropertyInfo();
            pi6.setType(decimal.class);
            pi6.setName("persons");
            pi6.setValue(persons);
            request.addProperty(pi6);

            SetEnvelope();
            //Create envelope
        }
    catch (Exception exception)
        {
        erorr= "error:"+exception.toString();
        }
    try
        {
            androidHttp.call(SOAP_ACTION, envelope);
            //Vector<String> result = null;
            //result = (Vector<String>) envelope.getResponse();
            String result = envelope.getResponse().toString();
            try { 
                id=Integer.parseInt(result); 
            } catch(NumberFormatException e) { 
                erorr=result;
            }
        }
    catch (Exception exception)
        {
        erorr= "error:"+exception.toString();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.