Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> android網絡編程:URL獲取數據和圖片

android網絡編程:URL獲取數據和圖片

編輯:Android開發教程

首先,開始最簡單的網絡編程實戰,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. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved