如何從android服務器中使用web服務獲取數據,並在下一頁的listview中顯示數據,單擊按鈕。什麼是方法的android應用程序連接到Web服務,在這裏我插入代碼conncet到web服務如何從android服務器中使用web服務獲取數據
這裏是我的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) {
}
}
});
}
這裏是我的web服務
[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;
}
參考,此[網絡錯誤](http://stackoverflow.com/questions/21062060/app-crashes-on-json-jparser-make-http-請求/ 21062282#21062282) –
替換此'最終字符串SOAP_ADDRESS =「http:// localhost:22781」您的網絡IP地址像「」http://198.1.1.20:22781「 –