2014-11-25 51 views
0

我想要做的是在ASP.NET中創建一個簡單的Web服務,並通過該Web服務從MSSQL服務器獲取數據,並在我的Android應用程序中使用該數據。作爲新手,我已經嘗試了這個Helpful的鏈接。我已經使用了本教程所提出的相同架構。但在我的結果中解析結果(肥皂響應)時出現錯誤。如下所示。如何從android中的SOAP響應中獲取數據?

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope 
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
> <soap:Body><findContactResponse xmlns="http://tempuri.org/" /> 
> </soap:Body> </soap:Envelope> 

和GetPropertyCount()方法返回0,另一方面,arrayindexoutofBound誤差未來與索引爲0

這是我的活動的代碼。我在哪裏實施了易於處理的異步任務。

public class MainActivity extends Activity { 

    /** Called when the activity is first created. */ 
    private static final String SOAP_ACTION = "http://tempuri.org/findContact"; 

    private static final String OPERATION_NAME = "findContact";// your webservice web method name 

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 

    private static final String SOAP_ADDRESS = "http://10.0.2.2:58497/WebService/Service.asmx?wsdl";//"http://127.0.0.1:58497/WebService/Service.asmx"; 

    protected static final String TAG = null; 

    TextView tvData1; 
    EditText edata; 
    Button button; 
    String studentNo; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tvData1 = (TextView)findViewById(R.id.textView1); 
     edata =(EditText)findViewById(R.id.editText1); 

     button=(Button)findViewById(R.id.button1); 

     button.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

        studentNo=edata.getText().toString(); 
        new Submit().execute(studentNo); 
      } 
     }); 
    } 

    private class Submit extends AsyncTask<String, Void, Void> { 

     @Override 
     protected void onPreExecute() { 

      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(String... arg) { 

      // TODO Auto-generated method stub 
      SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
      PropertyInfo propertyInfo = new PropertyInfo(); 
      propertyInfo.type = PropertyInfo.STRING_CLASS; 
      propertyInfo.name = "eid"; 

      Log.e("studentNo", studentNo); 
      request.addProperty(propertyInfo); 
      request.addProperty("eid", studentNo); 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 
      HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
      httpTransport.debug = true; 
      try { 

        httpTransport.call(SOAP_ACTION, envelope); 
        Log.e("Dump", httpTransport.responseDump.toString()); 
       SoapObject result=(SoapObject)envelope.bodyIn; 
       if(result!= null){ 

        Log.e("response", result.toString()); 
        //To get the data. 
        System.out.println("********Count : "+ result.getPropertyCount()); 
        String resultData=result.getProperty(0).toString(); 
        Log.e("Found", resultData); 
       } 
       else{ 

        Log.e("Obj", result.toString()); 
       } 
      } 
      catch (Exception exception) { 

       Log.e("Not Found", exception.toString()); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 

      super.onPostExecute(result); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 

      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
} 

現在任何人都可以告訴我哪裏出了問題嗎?或者我怎樣才能毫無例外地得到我的迴應?調試後,我想出了這條線正在創建異常。

String resultData=result.getProperty(0).toString(); 

非常感謝您提供任何幫助。

+0

http://programmingjungle.blogspot.com/2013/05/soap-client-on-android-using-jibx.html?m=1他們要麼是瘋狂的,要麼在Android上找到了一種簡單的肥皂方法 – Anthony 2014-11-25 09:33:28

+0

It對我沒有多大幫助。我正在尋找在數據解析的情況下發生araayindexOutofBound錯誤時應該怎麼做?或者我需要做些什麼來獲得從Soap Response @Anthony解析的數據 – user3673503 2014-11-26 06:06:06

回答

0

終於明白了。

這是在增加財產。稍微改變響應解析。

這是完整的代碼,爲我制定了。

希望它可以幫助別人。

private static final String SOAP_ACTION = "http://tempuri.org/findContact"; 

    private static final String OPERATION_NAME = "findContact";// your webservice web method name 

    private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 

    private static final String SOAP_ADDRESS = "http://10.0.2.2:58497/WebService/Service.asmx"; 

    protected static final String TAG = null; 
    private static String fahren; 

    TextView tvData1; 
    EditText edata; 
    Button button; 
    String studentNo; 
    String state; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tvData1 = (TextView)findViewById(R.id.textView1); 
     edata =(EditText)findViewById(R.id.editText1); 

     button=(Button)findViewById(R.id.button1); 

     button.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       studentNo=edata.getText().toString(); 
       new Submit().execute(studentNo); 
      } 
     }); 
    } 

    private class Submit extends AsyncTask<String, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

     } 

     @Override 
     protected String doInBackground(String... arg) { 
      // TODO Auto-generated method stub 
      SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
      PropertyInfo propertyInfo = new PropertyInfo(); 
      propertyInfo.type = PropertyInfo.STRING_CLASS; 
      propertyInfo.name = "eid"; 
      propertyInfo.setValue(studentNo); 
      request.addProperty(propertyInfo);//problem was here 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 
      HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
      httpTransport.debug = true; 
      try{ 
       httpTransport.call(SOAP_ACTION, envelope); 

       Log.e("RequestDump", httpTransport.requestDump.toString()); 
       Log.e("ResponseDump", httpTransport.responseDump.toString()); 

       SoapObject result=(SoapObject)envelope.bodyIn; 
       if(result!= null){ 
        state = result.getProperty(0).toString(); 
        Log.e("Found", state); 
       } 
       else{ 
        Log.e("Obj", result.toString()); 
       } 
      } 
      catch (Exception exception) { 
       Log.e("Exception", exception.toString()); 
      } 
      return state; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      tvData1.setText(result); 
     } 
    } 
} 

我已經實現了一個異步任務,以及擺脫對主線程運行時異常。需要正確添加request.addProperty(propertyInfo)。