zoukankan      html  css  js  c++  java
  • Android Get方式发送信息

    程序需要用到Internet权限,所以需要在AndroidManifest.xml添加

    <uses-permission android:name="android.permission.INTERNET"/>

    MainActivity.java

    public class MainActivity extends Activity {
        private TextView info=null;
        private Button Btn01=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            super.setContentView(R.layout.activity_main);
            this.info=(TextView)super.findViewById(R.id.info);
            this.Btn01=(Button)super.findViewById(R.id.Btn01);
            this.Btn01.setOnClickListener(new OnClickListenerImpl());
    
        }
        
        private class OnClickListenerImpl implements OnClickListener{
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println(">>Button");
                switch (v.getId()) {
                case R.id.Btn01:
                    try {
                        System.out.println(">>btn01");
                        //URL url=new URL("http","172.17.8.28",80,"android.ashx");
                        URL url=new URL("http","t.sina.com",80,"/");
                        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
                        byte data[]= new byte[512]; 
                        int len=conn.getInputStream().read(data);//输入流读取
                        System.out.println(">>len="+len);
                        if(len>0){
                            String temp= new String(data,0,len).trim();
                            //flag=Boolean.parseBoolean(temp); //取出里面的bool数据
                            System.out.println(">>"+temp);
                            MainActivity.this.info.setText(temp);
                        }
                        conn.getInputStream().close();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println("Error:"+e.toString());
                    }
                    break;
    
                default:
                    break;
                }
            }
            
        }

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
        <Button
            android:id="@+id/Btn01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="测试" />
        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
  • 相关阅读:
    Android学习关于setWidth()和setHeight()没反应的问题
    东芝c600T08B win7改装xp遇到的一些问题总结
    [转]java中long,int,short与byte数组之间的转换
    [转]简述STRUTS2 Convention零配置
    ie6下报错缺少标识符、字符串或数字 问题解决
    Android学习解决Android Graphical Layout 界面效果不显示
    [原创]tomcat6.0+IIS6+jk的配置
    Oracle中NVARCHAR2与VARCHAR2的区别
    优化like查询
    RedHat linux下安装hadoop 0.20.2, 并在windows下远程连接此hadoop,开发调试
  • 原文地址:https://www.cnblogs.com/taobox/p/3396116.html
Copyright © 2011-2022 走看看