public class HttpUtils {
public static String sendGet(String path) {
String content = null;
try {
// 设置访问url
URL url = new URL(path);
// 打开请求
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 设置请求的信息
httpURLConnection.setRequestMethod("GET");
// 设置请求是否超时时间
httpURLConnection.setConnectTimeout(5000);
// 判断服务器是否响应成功
if (httpURLConnection.getResponseCode() == 200) {
// 获取响应的输入流对象
InputStream is = httpURLConnection.getInputStream();
byte data[] = StreamTools.isToData(is);
// 把转换成字符串
content = new String(data);
// 内容编码方式
if (content.contains("gb2312")) {
content = new String(data, "gb2312");
}
}
// 断开连接
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static Bitmap sendGets(String path) {
Bitmap bitmap = null;
try {
URL url = new URL(path);
// 打开请求
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 设置请求的信息
httpURLConnection.setRequestMethod("GET");
// 设置请求是否超时时间
httpURLConnection.setConnectTimeout(5000);
// 判断服务器是否响应成功
if (httpURLConnection.getResponseCode() == 200) {
// 获取响应的输入流对象
InputStream is = httpURLConnection.getInputStream();
System.out.println("-----" + is);
// 直接把这个is流转换成Bitmap对象
bitmap = BitmapFactory.decodeStream(is);
}
// 断开连接
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}
public class MainActivity extends Activity {
// 声明控件
public EditText et_url;
public ImageView iv_ie;
// 网络操作类
public NetWorkUtils netWorkUtils;
private Handler handler;
public Bitmap bm;
public static final int TEXT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取et_url对象
et_url = (EditText) findViewById(R.id.et_url);
iv_ie = (ImageView) findViewById(R.id.iv_ie);
// 实例化
netWorkUtils = new NetWorkUtils(this);
// 实例化这个处理者
handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case TEXT:
iv_ie.setImageBitmap(bm);// 设置显示的文本
break;
default:
break;
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendHttp(View v){
//设置网络
boolean flag = netWorkUtils.setActiveNetWork();
if(flag){
//获取路径的值
final String path = et_url.getText().toString();
if(TextUtils.isEmpty(path)){
Toast.makeText(MainActivity.this, "访问的url地址不能为空", Toast.LENGTH_LONG).show();
}else{
//子 线程
new Thread(new Runnable() {
@Override
public void run() {
bm = HttpUtils.sendGets(path);
handler.sendEmptyMessage(TEXT);
}
}).start();
}
}
}
}
==========================
<EditText
android:id="@+id/et_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:text="@string/url_text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/et_url"
android:onClick="sendHttp"
android:text="@string/btn_text" />
<ImageView
android:id="@+id/iv_ie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:src="@drawable/ic_launcher" />
String文件
<string name="url_text">http://172.22.64.20:8080/test/images/0.jpg</string>
<string name="btn_text">浏览</string>
记得添加访问网络的权限 ,请参考上一篇文章