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;
}
First and for most thing for you is: You can't make a web API call or can't perform any long running task on main UI directly. FYI, you are making a web call in
onCreate()
method directly without implementing threading mechanism.You can implement
AsyncTask
to resolve this issue.Second, you can't use
http://localhost
because it refers to the device on which you are trying to test/run your application. Check for more about this issue: How to connect to my http://localhost web server from Android Emulator in Eclipse