Android中的Http通信(三)HttpUrlConnection的介绍

通过项目来介绍,直接上代码

先创建一个HttpThread类

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Agree on 2017/8/10.
 */
public class HttpThread extends Thread {

    private WebView webView;
    private String url;
    private Handler handler;
    private ImageView img;

    public HttpThread(String url,WebView webView,Handler handler){
        this.url=url;
        this.webView=webView;
        this.handler=handler;
    }
    public HttpThread(String url,ImageView img,Handler handler){
        this.url=url;
        this.img=img;
        this.handler=handler;
    }

    @Override
    public void run() {
        try {
            URL httpUrl=new URL(url);
            HttpURLConnection connection= (HttpURLConnection) httpUrl.openConnection();
            connection.setReadTimeout(5000);
            connection.setRequestMethod("GET");
            final StringBuffer sb=new StringBuffer();
            BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String str;
            while ((str=reader.readLine())!=null){
                sb.append(str);
            }

            handler.post(new Runnable() {
                @Override
                public void run() {
                webView.loadData(sb.toString(),"text/html;charset=utf-8",null);
                }
            });

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.run();
    }
}
主Activity中的代码

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private ImageView img;
    private Handler handler=new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        img = (ImageView) findViewById(R.id.imageView);

        new HttpThread("http://www.vko.cn",webView,handler).start();
    }
}
用HttpUrlConnection来加载图片

URL httpUrl=new URL(url);
HttpURLConnection connection= (HttpURLConnection) httpUrl.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
InputStream in=connection.getInputStream();
FileOutputStream out=null;
File downLoadFile=null;
String fileName= String.valueOf(System.currentTimeMillis());
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    File parent=Environment.getExternalStorageDirectory();
    downLoadFile=new File(parent,fileName);
    out=new FileOutputStream(downLoadFile);
}
byte[] b=new byte[2*1024];
int len;
if (out!=null){
    while ((len=in.read(b))!=-1){
        out.write(b,0,len);
    }
}
final Bitmap bitmap= BitmapFactory.decodeFile(downLoadFile.getAbsolutePath());
handler.post(new Runnable() {
    @Override
    public void run() {
        img.setImageBitmap(bitmap);
    }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值