Calling A Web Service From An Android Application
I am using the following code which i found on internet to call a web service from my android app: public class WebServiceActivity extends Activity { private static final Strin
Solution 1:
Put your web service request as a background task. Usually time taken by a network activity is unpredictable. If you use above, the UI Thread will block. That is why it says "Application not responding".
I found this great article. http://www.vogella.de/articles/AndroidPerformance/article.html#concurrency Have a look at 2. Background Processing
Your example can be modified as following.
publicclassWebServiceActivityextendsActivity {
privatestaticfinalStringSOAP_ACTION="http://tempuri.org/HelloWorld";
privatestaticfinalStringMETHOD_NAME="HelloWorld";
privatestaticfinalStringNAMESPACE="http://tempuri.org/";
privatestaticfinalStringURL="http://192.168.1.19/TestWeb/WebService.asmx";
TextView result1;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result1 = (TextView) findViewById(R.id.result1);
Buttongetquote= (Button) findViewById(R.id.getquote);
getquote.setOnClickListener(newOnClickListener() {
publicvoidonClick(View v) {
// push soap request into background.newThread(newRunnable(){
@Overridepublicvoidrun() {
doSoapRequest();
}
},"DOINBACKGROUND");
}
});
}
privatevoiddoSoapRequest(){
try {
SoapObjectrequest=newSoapObject(NAMESPACE, METHOD_NAME);
EditTextCompanyName= (EditText) findViewById(R.id.CompanyName);
Stringval1= (CompanyName.getText().toString());
request.addProperty("passonString", val1);
SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSEandroidHttpTransport=newHttpTransportSE(
URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Objectresult= (Object) envelope.getResponse();
// result1.setText(result.toString());// update UI data in a HandlerMessagemsg=newMessage();
msg.obj = result.toString();
result1Handler.sendMessage(msg);
} catch (Exception e) {
// result1.setText(e.getMessage());// update UI data in a HandlerMessagemsg=newMessage();
msg.obj = e.getMessage();
result1Handler.sendMessage(msg);
}
}
privateHandlerresult1Handler=newHandler(){
@OverridepublicvoidhandleMessage(Message msg) {
super.handleMessage(msg);
result1.setText(msg.obj.toString());
}
};
}
Post a Comment for "Calling A Web Service From An Android Application"