获取网络数据
1.URL地址
URL url = new URL ("http://www.baidu.com/");
URL url = URL ("http://www.baidu.com/");
2.建立连接
HttpURLConnection urlConnection = (HttpURLConnection )url.openConnection;
3.连接成功判断
if(urlConnection .getResponseCode()==200)
{
Log.i("test","连接成功");
}
4.开始传输数据获取相应的对象流
InputStream in = urlConnection .getInputStream()
5.如果操作成功,应关闭对象,关闭连接
urlConnection .disconnect()
完整例子
package com.example.dz.mygrame;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by DZ on 2017/11/24.
*/
public class mymain extends Activity {
private ImageView ivDown ;
private Button btDown ;
private int ERROR=2;
private int i=0;
private String [] str = {"https://gss0.bdstatic.com/7Po4cT78BgN3otqbppnN2DJv/baike/abpic/item/b8389b504fc2d56206e31a90e31190ef77c66ce0.jpg?k=img_47",
"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3036132189,30620540&fm=27&gp=0.jpg",
"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2346337118,3956192888&fm=27&gp=0.jpg",
"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2827887652,1878401612&fm=27&gp=0.jpg"};
//图片地址
private Handler handler = new Handler(){
//修改界面
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1)
{
ivDown.setImageBitmap((Bitmap) msg.obj);
}else if(msg.what==ERROR)
{
Toast.makeText(mymain.this,"失败",Toast.LENGTH_SHORT).show();
}
// ivDown.setBackground((Drawable) msg.obj);
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intern);
ivDown = (ImageView) findViewById(R.id.iv_down);
btDown = (Button) findViewById(R.id.btn_down);
btDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
* 主线程中不允许访问网络
* 访问网络需要加权限//
*
* */
new mythread().start();
i= ((i+1)%4);
}
});
}
private class mythread extends Thread{
@Override
public void run() {
/* 1.URL地址
URL url = new URL ("http://www.baidu.com/");
URL url = URL ("http://www.baidu.com/");
2.建立连接
HttpURLConnection urlConnection = (HttpURLConnection )url.openConnection;
3.连接成功判断
if(urlConnection .getResponseCode()==200)
{
Log.i("test","连接成功");
}
4.开始传输数据获取相应的对象流
InputStream in = urlConnection .getInputStream()
5.如果操作成功,应关闭对象,关闭连接
urlConnection .disconnect()*/
Log.i("test","子线程开始运行");
Bitmap bitmap = null ;
InputStream in = null;
try {
URL url = new URL (str[i]);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if(conn.getResponseCode()==200){
Log.i("test","连接成功");
in = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(in);
Message msg = new Message();
msg.obj = bitmap;
msg.what=1;
handler.sendMessage(msg);
/*byte[] bytes=new byte[1024];
in.read(bytes);
Log.i("test",String.valueOf(bytes));*/
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.obj = bitmap;
msg.what=ERROR;
handler.sendMessage(msg);
}finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
网友评论