如何在Android中使用Web服务来获取数据从SQL Server [英] how to get data from sql server using web service in android

查看:142
本文介绍了如何在Android中使用Web服务来获取数据从SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得在Android中使用Web服务从SQL Server数据并显示,在列表视图在接下来的页面上点击按钮的数据。什么是方法的android应用程序连接到Web服务,在这里我插入code到conncet到web服务

下面是我的activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@可绘制/ AB>

<的TextView
    机器人:ID =@ + ID / text_persons
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginLeft =19dp
    机器人:layout_marginTop =90dp
    机器人:文本=@字符串/ text_persons
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的TextView
    机器人:ID =@ + ID / text_amount
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / text_persons
    机器人:layout_marginTop =46dp
    机器人:layout_toLeftOf =@ + ID / edit_persons
    机器人:文本=@字符串/ text_amount
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

<的EditText
    机器人:ID =@ + ID / edit_persons
    机器人:layout_width =175dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBottom =@ + ID / text_persons
    机器人:layout_marginLeft =14dp
    机器人:layout_toRightOf =@ + ID / text_persons
    机器人:EMS =10
    机器人:提示=@字符串/ edit_persons
    机器人:inputType =号/>

<的EditText
    机器人:ID =@ + ID / edit_amount
    机器人:layout_width =175dp
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignBottom =@ + ID / text_amount
    机器人:layout_alignLeft =@ + ID / edit_persons
    机器人:EMS =10
    机器人:提示=@字符串/ edit_amount
    机器人:inputType =号/>

<按钮
    机器人:ID =@ + ID / button_findfood
    机器人:layout_width =200dp
    机器人:layout_height =45dp
    机器人:layout_below =@ + ID / text_amount
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =54dp
    机器人:文本=@字符串/ button_findfood/>

<按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =200dp
    机器人:layout_height =45dp
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginBottom =64dp
    机器人:文本=@字符串/ button_map/>
 

这是我的mainactivity.java`

 公共类MainActivity延伸活动{

的EditText editPersons,editAmount;
字符串youramount,yourpersons;
// KSOAP
        最后弦乐SOAP_ACTION =htt​​p://tempuri.org/Products;
        最后弦乐METHOD_NAME =产品;
        最后弦乐WSDL_TARGET_NAMESPACE =htt​​p://tempuri.org/;
        最后弦乐SOAP_ADDRESS =HTTP://本地主机:22781 / WebService.asmx OP =产品?;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);


    //转到下一页
    Button按钮=(按钮)findViewById(R.id.button_findfood);
    button.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            意图I =新的意图(getApplicationContext(),DisplayMessageActivity.class);
            startActivity(ⅰ);

    SoapObject请求=新SoapObject(WSDL_TARGET_NAMESPACE,METHOD_NAME);
    的PropertyInfo的PropertyInfo =新的PropertyInfo();
    propertyInfo.name =量;
    propertyInfo.name =人;

    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包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = TRUE;
        envelope.setOutputSoapObject(要求);

        HttpTransportSE httpTransport =新HttpTransportSE(SOAP_ADDRESS);

        尝试  {
            httpTransport.call(SOAP_ACTION,包);
            对象响应= envelope.getResponse();
        }赶上(例外的例外){

   }
        }
    });

}
 

这里是我的web服务

  [WebMethod的]
公共数据集产品(十进制量,小数人)
{

    十进制价格=量/人;
    DataSet的结果= NULL;
    常量字符串SQL_COMMAND_TEXT =SELECT菜单,价格从ASD在价格< = @price;
    使用(SqlConnection的连接= WebSerConnection.GetConnection())
    {
        connection.Open();
        使用(SqlCommand的命令=新的SqlCommand(SQL_COMMAND_TEXT,连接))
        {
            command.Parameters.Add(@人,SqlDbType.VarChar);
            command.Parameters.Add(@价格,SqlDbType.Int);
            command.Parameters [@人]值=人。
            command.Parameters [@价]值=价格。
            使用(SqlDataAdapter的DataAdapter的=新的SqlDataAdapter(命令))
            {
                结果=新的DataSet();
                DataAdapter.Fill方法(结果);
            }
        }
    }

    返回结果;
}
 

解决方案

 公共类CallSoap
{

公共字符串SOAP_ACTION =htt​​p://tempuri.org/CreateEvent;

公共字符串OPERATION_NAME =CreateEvent的;

公共最后弦乐WSDL_TARGET_NAMESPACE =htt​​p://tempuri.org/;

公共字符串erorr =;

公共最后弦乐SOAP_ADDRESS =HTTP://xxxx/Service1.asmx;

SoapObject请求;
SoapSerializationEnvelope信封;

// AndroidHttpTransport androidHttpTransport;
HttpTransportSE androidHttp;

公共CallSoap()
{
}


保护无效SetEnvelope(){

    尝试 {
        。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
        StrictMode.setThreadPolicy(政策);

        //创建SOAP信封
        信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);

        //可以评论说,行了,如果您的Web服务是不是.NET之一。
        envelope.dotNet = TRUE;

        envelope.setOutputSoapObject(要求);
        androidHttp =新HttpTransportSE(XXXX / Service1.asmx的);

        androidHttp.debug = TRUE;

    }赶上(例外五){
        的System.out.println(肥皂异常---->>>中+ e.toString());
    }
}

@燮pressLint(SimpldurationFormat)
公众诠释发送(十进制量,小数人)
{
    INT标识= 0;
    尝试{
            //请求
            要求=新SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

            的PropertyInfo PI5 =新的PropertyInfo();
            pi5.setType(decimal.class);
            pi5.setName(额);
            pi5.setValue(金额);
            request.addProperty(PI5);

            的PropertyInfo PI6 =新的PropertyInfo();
            PI6 =新的PropertyInfo();
            pi6.setType(decimal.class);
            pi6.setName(人);
            pi6.setValue(人);
            request.addProperty(PI6);

            SetEnvelope();
            //创建信封
        }
    赶上(例外的例外)
        {
        erorr =错误:+ exception.toString();
        }
    尝试
        {
            androidHttp.call(SOAP_ACTION,包);
            //矢量<串GT;结果= NULL;
            //结果=(矢量<字符串&GT)envelope.getResponse();
            。字符串结果= envelope.getResponse()的toString();
            尝试 {
                ID =的Integer.parseInt(结果);
            }赶上(NumberFormatException异常E){
                erorr =结果;
            }
        }
    赶上(例外的例外)
        {
        erorr =错误:+ exception.toString();
        }
    }
 

how do i get data from sql server using web service in android and show that data in listview in next page on click the button. what is the method to connect android application to webservices, where i insert code to conncet to the webservices

Here is my 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" />

here is my 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)   {

   }            
        }
    });

}

here is my web service

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

解决方案

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

这篇关于如何在Android中使用Web服务来获取数据从SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆