Android HTTP协议(一)

之前在自己公司做的手机OS上开发,最痛苦的就是我们的系统没有现成的HTTP协议,只有使用开源库的libCurl进行封装,在此过程中很好的熟悉了HTTP请求的一些细节,现在想想还是不错的一个经历,现在转到Android上了,对于Google来说,如果联网都处理不好的话,号称最好的互联网公司就太逊了吧。

 使用URLConnection示例

下载html文件,放到文本控件中显示

public  class AndroidNetActivity  extends Activity {
    
    Handler handler;
     /**  Called when the activity is first created.  */
    @Override
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         final EditText eText = (EditText)findViewById(R.id.address);
         final TextView tView = (TextView)findViewById(R.id.pagetext);
        
        handler =  new Handler(){
             public  void handleMessage(Message msg){
                 switch(msg.what)
                {
                     case 0:
                    {
                        tView.append((String)msg.obj);
//                         tView.setText((String)msg.obj);
                         break;
                    }
                }
                 super.handleMessage(msg);
                
            }
            
        };
        
         final Button button = (Button)findViewById(R.id.ButtonGo);
        button.setOnClickListener( new Button.OnClickListener(){

            @Override
             public  void onClick(View v) {
                 //  TODO Auto-generated method stub
                 try{
                    tView.setText("");
                    URL url =  new URL(eText.getText().toString());
                    URLConnection conn = url.openConnection();  
//                      // 方式1
                    BufferedReader rd =  new BufferedReader(
                             new InputStreamReader(conn.getInputStream()));        
                    
                    String line = "";
                     while((line = rd.readLine())!=  null)
                    {
                        Message lmsg;
                        lmsg =  new Message();
                        lmsg.what = 0;                        
                        lmsg.obj = line;
                        AndroidNetActivity. this.handler.sendMessage(lmsg);
                    }
                     // 方式2
//                     InputStream is = null;
//                     is = conn.getInputStream();
//                     byte[] result=null;
//                     
//                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
//                     while(true)
//                     {
//                         byte[] bytes = new byte[64];
//                         int tmp = is.read(bytes);
//                         if(tmp == -1)
//                         {
//                             break;
//                         }
//                         else
//                         {
//                             bos.write(bytes,0,tmp);
//                         }
//                     }
//                     result = bos.toByteArray();                    
//                     String all;
//                     all = new String(result);
//                     
//                     Message msg;
//                     msg = new Message();
//                     msg.what = 0;                        
//                     msg.obj = all;
//                     AndroidNetActivity.this.handler.sendMessage(msg);
               }
                 catch(Exception e)
                {
                    e.printStackTrace();
                    }
                
            }
            
        });
    }
}

 

 HttpURLConnection

 继承自URLConnection ,相当于封装的HTTP协议吧。示例代码

 

    HttpURLConnection  conn= null;
    URL url;
    conn=(HttpURLConnection)url.openConnection();
    conn.setDoInput( true);   
    conn.setDoOutput( true);
    conn.setRequestMethod("GET");  
    conn.setRequestProperty("Accept", "application/octet-stream, */*");
    conn.setRequestProperty("Connection", "Keep-Alive");
    conn.setRequestProperty("Content-Type","application/octet-stream");
    conn.connect();
     int code=conn.getResponseCode();
     int len=conn.getContentLength();

     if(conn== null)
    {
         return  null;
    }
    InputStream is= null;
     byte[] result= null;
    ByteArrayOutputStream bos= new ByteArrayOutputStream();
    is=conn.getInputStream();
     while( true){
         byte[] bytes =  new  byte[64];
         int tmp=is.read(bytes);
         if(tmp==-1)
        {
             break;
        }
         else
        {
            bos.write(bytes, 0, tmp);
        }
        }
    result = bos.toByteArray();

 

下次整理一下HTTPClient的

 

 

 

 

转载于:https://www.cnblogs.com/alwaysyouare/archive/2011/11/10/2244666.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值