android网络编程,是我们学习网络开发的重要课程,在此小弟今天学习了一下,从互联网上获取图片与门户网的源代码。大家肯定很急了吧,先让我们看一下效果先,大家还是很感兴趣的。
如图:
我的宝马牛吧,虚拟的,真的就好了。
让我们看一下原代码是如何写。这可是我的原创哦
- package com.smart.net.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import com.smart.net.utils.NetTool;
- 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);
- }
- });
- }
- }
网页类
- package com.smart.net.activity;
- import com.smart.net.utils.NetTool;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- 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();
- }
- }
- }
方法实现类
- package com.smart.net.utils;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- 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);
- // 别超过10秒。
- 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);
- // 别超过10秒。
- 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();
- }
- }