2011-09-30 121 views
0

我使用下面的代碼,我發現互聯網上,從我的Android應用程序調用Web服務:調用從Android應用程序的網絡服務

public class WebServiceActivity extends Activity { 
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld"; 

    private static final String METHOD_NAME = "HelloWorld"; 

    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx"; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 



     Button getquote = (Button) findViewById(R.id.getquote); 
     getquote.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      TextView result1; 
      result1=(TextView)findViewById(R.id.result1); 
     try { 


      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      EditText CompanyName = (EditText) findViewById(R.id.CompanyName); 
      String val1 = (CompanyName.getText().toString()); 
      request.addProperty("passonString", val1); 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet=true; 
      envelope.setOutputSoapObject(request); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      Object result = (Object)envelope.getResponse(); 

      result1.setText(result.toString()); 
     } catch (Exception e) { 

      result1.setText(e.getMessage()); 
      } 
    } 
}); 

} 

}

我收到錯誤的應用沒有迴應。我是新來的,請幫忙。 在此先感謝

+0

ANR在「主」線程中發生某些長操作時發生。這是事件循環線程,如果它很忙,Android無法在應用程序中處理任何進一步的GUI事件,從而拋出一個ANR對話框..你必須通過這個:http://developer.android.com/guide /practices/design/responsiveness.html –

回答

0

將您的Web服務請求作爲後臺任務。通常,網絡活動所花費的時間是不可預測的。如果你在上面使用,UI線程將被阻塞。這就是爲什麼它說「應用程序沒有響應」。

我發現這篇文章很棒。 http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency 看看2.背景處理

你的例子可以修改如下。

public class WebServiceActivity extends Activity { 
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld"; 

    private static final String METHOD_NAME = "HelloWorld"; 

    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx"; 

    TextView result1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     result1 = (TextView) findViewById(R.id.result1); 

     Button getquote = (Button) findViewById(R.id.getquote); 
     getquote.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       // push soap request into background. 
       new Thread(new Runnable(){ 

        @Override 
        public void run() { 
         doSoapRequest(); 
        } 

       },"DOINBACKGROUND"); 
      } 
     }); 

    } 

    private void doSoapRequest(){ 
     try { 

      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      EditText CompanyName = (EditText) findViewById(R.id.CompanyName); 
      String val1 = (CompanyName.getText().toString()); 
      request.addProperty("passonString", val1); 

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(
        URL); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 

      Object result = (Object) envelope.getResponse(); 

//   result1.setText(result.toString()); 
//   update UI data in a Handler 
      Message msg = new Message(); 
      msg.obj = result.toString(); 
      result1Handler.sendMessage(msg); 
     } catch (Exception e) { 

//   result1.setText(e.getMessage()); 
//   update UI data in a Handler 
      Message msg = new Message(); 
      msg.obj = e.getMessage(); 
      result1Handler.sendMessage(msg); 
     } 
    } 

    private Handler result1Handler = new Handler(){ 
     @Override 
     public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      result1.setText(msg.obj.toString()); 
     } 

    }; 

} 
+0

你能給我一些更具體的例子嗎?使用處理程序,我發現它有點難以理解,但我試圖理解它。 – Ritu

+1

我舉了一個例子。試着瞭解發生了什麼。如果你想在上面看到一個進度條,它應該如何修改?提示:顯示進度條也是應該在UI線程中完成的。 – prageeth