package com.yanjun;

import com.yanjun.getImage.GetHTML;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* 通过网络获取图片
* @author YanJun
*<uses-permission android:name="android.permission.INTERNET"></uses-permission>
*/
public class MainActivity extends Activity {


private static final String TAG = "MainActivity";
EditText htmlEditText = null ;
Button button ;
TextView htmlTextView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
htmlTextView = (TextView) findViewById(R.id.textView_html);
htmlEditText = (EditText) findViewById(R.id.editText_html);
button = (Button) findViewById(R.id.button_get);
button.setOnClickListener( new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
String Url = htmlEditText.getText().toString();
try {
htmlTextView.setText(GetHTML.getHtml(Url));
} catch (Exception e) {

Toast
.makeText(MainActivity. this, "链接延时",
Toast.LENGTH_LONG).show();
Log.e(TAG, e.toString());
}
}
});
}

} package com.yanjun.getImage;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetHTML {
/**
* 获取路径方法
*
* @param path 路径
* @return
* @throws Exception
*/
public static String getHtml(String path) throws Exception {
// 从网络上获取html--URL对象用来封装路径
URL url = new URL(path);
// 打开路径链接---得到HttpURLConnection对象
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 通过HTTP协议请求网络HTML---设置请求方式:get/post
httpURLConnection.setRequestMethod( "GET");
// 设置连接超时
httpURLConnection.setConnectTimeout(5 * 1000);
// 从外界想手机应用内传递数据----通过输入流获取HTML数据
InputStream inputStream = httpURLConnection.getInputStream();
byte[] data = StreamTool.readInputStream(inputStream);
// 从输入流中获取HTML的二进制数据----readInputStream()
String html = new String(data);

return html;

}
}
package com.yanjun.getImage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {

/**从输入流获取数据
* @param inSream 输入流
* @return
* @throws Exception
*/

public static byte[] readInputStream(InputStream inSream) throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 定义一个缓冲区
byte[] buffer = new byte[1024];
int len = 0;
// 不断的从流里读取数据---while循环---nSream.read(buffer)表示从流里读取数据到缓冲区
// 读取到末尾时,返回值是-1;
while ((len = inSream.read(buffer)) != -1) {
// 将缓冲区的数据写到输出流中
byteArrayOutputStream.write(buffer, 0, len);
}
inSream.close();
return byteArrayOutputStream.toByteArray();
}
}

更多相关文章

  1. android 入门demo 事件监听
  2. 【Android】【应用信息】获取服务列表
  3. Android通过映射id获取view,resource资源
  4. Android之如何获取视频或者图片的缩略图
  5. android 获取WebView的网页高度
  6. [Android实例] android json
  7. android从网络获取图片以及异步加载图片
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. python起点网月票榜字体反爬案例

随机推荐

  1. Android(安卓)Studio 在活动中使用Toast
  2. Android widget使用
  3. Total Commander 公测第三版发布[Android
  4. 01.Android相关术语
  5. Android中Bitmap和Drawable互相转化
  6. Android初识之Activity组件
  7. android View System touch 事件分发流程
  8. Android中显示,隐藏Status Bar和Navigatio
  9. Android和Android Linux kernel源码获取
  10. iOS这几个方面该向Android好好学习