源码:
package cn.edu.bzu.a18lab12;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView etUrl;
ImageView ivNetImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUrl=(TextView)findViewById(R.id.etUrl);
etUrl.getPaint().setFlags(Paint.DITHER_FLAG);
}
public void showImage(View view){
String path=etUrl.getText().toString();
if(TextUtils.isEmpty(path)){//如果path为空
Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();
}else {
//连接服务器,发送请求
try {
URL url=new URL(path);
//发出http请求
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
//设置链接超时时间
httpURLConnection.setConnectTimeout(5000);
int responsecode=httpURLConnection.getResponseCode();
if(responsecode==200){
InputStream is=httpURLConnection.getInputStream();
Bitmap bitmap= BitmapFactory.decodeStream(is);
ivNetImage.setImageBitmap(bitmap);
}else {
Toast.makeText(this,"显示图片失败",Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package cn.edu.bzu.health;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Administrator on 2017/5/26.
*/
public class HealthWebView {
private Context context;
public HealthWebView(Context context) {
this.context = context;
}
public View getView(String url) {
View view = LayoutInflater.from(context).inflate(R
.layout.pagerofhealth,null);
WebView webView = (WebView) view.findViewById(R.id.wvHealth);
webView.loadUrl(url);
// 设置支持JavaScript脚本
webView.getSettings().setJavaScriptEnabled(true);
/**
* 禁止系统浏览器打开页面
*/
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
return view;
}
}
package cn.edu.bzu.health.adapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by Administrator on 2017/5/26.
*/
public class HealthViewPagerAdapter extends PagerAdapter{
private List
viewList;
public HealthViewPagerAdapter(List
viewList) {
this.viewList = viewList;
}
@Override
public int getCount() {
return viewList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
}
除此之外还需设置权限
在配置文件中application后加上<uses-permission android:name="android.permission.INTERNET"/> 这句话