android 网络编程--URL获取数据/图片
2013-05-14 22:00
302 查看
首先,开始最简单的网络编程实战,URL实现网络连接,不懂的童鞋可以参考J***A中的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(); } }
相关文章推荐
- android 网络编程--URL获取数据/图片
- android HttpURLConnection编程详解,获取网络图片,html代码查看
- android HttpURLConnection编程详解,获取网络图片,html代码查
- Android4.0.4编程日记(5)--URL网络链接获取数据
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例
- Android编程获取图片数据的方法详解
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- android 从网络上获取数据(图片,网页,xml,json等)
- Android--消息机制--handler--getImageFromUrl--网络上获取图片
- 【Android前端】Bing每日图片列表应用制作记录——4.获取网络数据(下)
- Android HttpURLConnection获取网络下载图片POST请求方式
- Android中如何根据图片url路径来获取网络图片
- URL转Drawable之 Android中获取网络图片的三种方法
- (转)Android通过URL获取网络数据
- URL转Drawable之 Android中获取网络图片的三种方法
- Android数据解析之JSON,网络编程之URL,异步任务AsyncTask讲解
- C# 网络编程之webBrowser获取网页url和下载网页中图片
- Android数据存储:获取网络图片把图片保存到SD卡中并从SDk卡中读取(通过Bitmap方法)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)