Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
how do i get data from sql server using web services in android and show that data in listview in next page by click button. i am get stuck plzz help...

Here is my activity_main.xml
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:removed="@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" />

</RelativeLayout>


here is MainActivity.java
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
C#
[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;
}
Posted
Comments
thatraja 6-Feb-14 10:53am    
You have mentioned code, ui things. But forgot to post about the issue. Any error? Include those details

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900