android 网络编程--URL获取数据/图片

首先,开始最简单的网络编程实战,URL实现网络连接,不懂的童鞋可以参考JAVA中的URL编程,其原理是一样的,在这里不再多做解释。

直接贴出实现源代码:

public class DataActivity extends Activity {  
    private EditText imagePathText;  
    private static final String TAG="DataActivity";  
    private ImageView imageView;  
 
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        imagePathText = (EditText) findViewById(R.id.imagepath);  
        imageView=(ImageView)findViewById(R.id.imageView);   
          
         
        Button button = (Button) findViewById(R.id.button);  
        button.setOnClickListener(new View.OnClickListener() {  
 
            @Override 
            public void onClick(View v) {  
                String path = imagePathText.getText().toString();  
                try {  
                      
                    byte[] data=NetTool.getImage(path);  
                    Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);  
                    imageView.setImageBitmap(bm);  
                } catch (Exception e) {  
                    Log.i(TAG, e.toString());  
                    Toast.makeText(DataActivity.this, "获得图片失败", 1).show();  
                }  
            }  
        });  
          
//      获取网页源代码  
        Button sinaButton = (Button) findViewById(R.id.sinaButton);  
        sinaButton.setOnClickListener(new View.OnClickListener() {  
 
            @Override 
            public void onClick(View v) {  
                Intent intent=new Intent(DataActivity.this,SinaActivity.class);  
                startActivity(intent);  
            }  
        });  
 
    }  
} 

public class SinaActivity extends Activity {  
    private TextView textView;  
    private static final String TAG="SinaActivity";  
//  获取网页源代码  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.sina);  
          
        textView=(TextView)this.findViewById(R.id.sina);  
        try {  
            String html=NetTool.getHtml("http://www.sina.com.cn","GBK");  
            textView.setText(html);  
        } catch (Exception e) {  
            Log.i(TAG, e.toString());  
            Toast.makeText(SinaActivity.this, "获得网页失败", 1).show();  
        }  
    }  
}  

public class NetTool {  
    /**  
     * 获得url代码数据  
     * */ 
      
    public static String getHtml(String path,String encoding) throws Exception {  
        URL url = new URL(path);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setRequestMethod("GET");  
        conn.setConnectTimeout(6 * 1000);  
        // 别超过6秒。  
        System.out.println(conn.getResponseCode());  
        if(conn.getResponseCode()==200){  
            InputStream inputStream=conn.getInputStream();  
            byte[] data=readStream(inputStream);  
            return new String(data,encoding);  
        }  
        return null;  
    }  
      
/**  
 * 获取指定路径,的数据。  
 *   
 * **/ 
    public static byte[] getImage(String urlpath) throws Exception {  
        URL url = new URL(urlpath);  
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
        conn.setRequestMethod("GET");  
        conn.setConnectTimeout(6 * 1000);  
        // 别超过6秒。  
        if(conn.getResponseCode()==200){  
            InputStream inputStream=conn.getInputStream();  
            return readStream(inputStream);  
        }  
        return null;  
    }  
      
    /**  
     * 读取数据   
     * 输入流  
     *   
     * */ 
    public static byte[] readStream(InputStream inStream) throws Exception {  
        ByteArrayOutputStream outstream=new ByteArrayOutputStream();  
        byte[] buffer=new byte[1024];  
        int len=-1;  
        while((len=inStream.read(buffer)) !=-1){  
            outstream.write(buffer, 0, len);  
        }  
        outstream.close();  
        inStream.close();  
          
    return outstream.toByteArray();  
}  
}  



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值