2014-02-05 104 views
1

如何從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; 
} 
+0

參考,此[網絡錯誤](http://stackoverflow.com/questions/21062060/app-crashes-on-json-jparser-make-http-請求/ 21062282#21062282) –

+0

替換此'最終字符串SOAP_ADDRESS =「http:// localhost:22781」您的網絡IP地址像「」http://198.1.1.20:22781「 –

回答

0
  1. 首先,對於大多數人來說,您是:您無法進行Web API調用,也無法直接在主UI上執行任何長時間運行的任務。僅供參考,您直接在onCreate()方法中進行網絡調用,而不實施線程機制。

    您可以執行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(); 
     } 
    } 
相關問題