Android与WEBservice通信报错

android.os.NetworkOnMainThreadException

在主线程中请求网络操作,将会抛出此异常。Android这个设计是为了防止网络请求时间过长而导致界面假死的情况发生。解决方案有两个,一个是使用StrictMode,二是使用线程来操作网络请求。

1.强制修改代码,仅作测试用

在setContentView(R.layout.main);添加

if (android.os.Build.VERSION.SDK_INT > 9) {
   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
   StrictMode.setThreadPolicy(policy);

2.将请求网络资源的代码使用Thread去操作,在Runnable中做HTTP请求。另外:Android禁止其他子线程来更新由UI thread创建的视图。命令通过Message传递。

public class MainActivity extends Activity {

private static final String serviceNameSpace="http://tempuri.org/";
private static final String methodName="HelloWorld";
//No.5 构建传输对象,并指明WSDL文档的URL
//请求URL
private static final String serviceURL="http://10.0.2.2:55493/TEST_website/Service.asmx";
private static final String soapAction="http://tempuri.org/HelloWorld";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*if (android.os.Build.VERSION.SDK_INT > 9) {
   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
   StrictMode.setThreadPolicy(policy);
   //StrictMode.setThreadPolicy(policy);
}
*///shiyongStrictMode,强制修改代码,不推荐
//将请求网络资源的代码使用Thread去操作,在Runnable中做HTTP请求,不用阻塞UI线程
Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
Looper looper=Looper.myLooper();
MessageHandler myhandler=new MessageHandler(looper);
String result;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//TextView resultText=(TextView) findViewById(R.id.textView1);
//线程执行结束后自动关闭
Runnable runnable=new Runnable(){
public void run(){
SoapObject request=new SoapObject(serviceNameSpace, methodName);
//实例化SoapObject对象
SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
//获得序列化的Envelope
envelope.bodyOut=request;
envelope.dotNet=true;//设置是否笤俑dotNet开发的WebService
envelope.setOutputSoapObject(request);
HttpTransportSE transport=new HttpTransportSE(serviceURL);
//调用WebService
try{
transport.call(soapAction, envelope);

}catch(Exception e){
e.printStackTrace();
}
//获得返回的数据,将返回的xml格式内容,转换成SoapObject对象
SoapObject object=(SoapObject)envelope.bodyIn;
//获取返回的结果
result=object.getProperty(0).toString();
//创建Message对象,把返回值发给Message
Message message=Message.obtain();
message.obj=result;
//通过handler发布消息
myhandler.sendMessage(message);
}
};
Thread mythread=new Thread(runnable);
mythread.start();
}
});
}

class MessageHandler extends Handler{
public MessageHandler(Looper looper){
super(looper);

@Override
public void handleMessage(Message msg){
TextView testresult=(TextView)findViewById(R.id.textView1);
testresult.setText(msg.obj.toString());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值