package com.example.imgwatcher;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
}
public void click(View v){
System.out.println("---");
//请求网址获取图片
//确定网址
String path = "http://192.168.68.232:8080/question/p1.jpg";
//获取URL对象
try {
URL url = new URL(path);
//获取连接对象
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//初始化连接对象
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);
}else{
Toast.makeText(this, "请求异常", 0).show();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}