android 4 访问web service的一个简单例子

package com.example.notification;


import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;


public class MainActivity extends Activity {
   private TextView   info=null;
   private Handler handler=null;
   
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
        this.info=(TextView)super.findViewById(R.id.info);
        getWebSource();
   this.handler=new Handler(){


@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println("msg的大小事"+msg.what);
if(msg.what==0){
info.setText("服务器异常");

}else if(msg.what==1){
info.setText("登陆成功");
}
else{
info.setText("失败");
}
}

};
    
}
private void getWebSource(){
new Thread(new Runnable(){


@Override
public void run() {
// TODO Auto-generated method stub
Message msg=new Message();
try {
URL url=new URL("http","192.168.1.50",8080,"/AndroidWebServices/index.jsp?name=ws&&password=12345");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
byte[] data=new byte[1024];
int len=conn.getInputStream().read(data);
String temp=new String(data,0,len);
System.out.println("服务器的内容是"+temp);

if(len>0){
msg.what=1;
handler.sendMessage(msg);
}
conn.getInputStream().close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
msg.what=0;
handler.sendMessage(msg);
}




}

}).start();;
}


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值