我正在使用以下代码在互联网上从我的Android应用程序调用Web服务:
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 :(得分: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());
}
};
}