网络权限已经添加了,就是运行起来点击按钮没反应
这是java代码
package com.example.activity;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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{
private Button bt;
private ImageView iv;
Handler handler=new Handler();
public void handleMessage(Message msg){
if(msg.what==1){
Bitmap bt=(Bitmap) msg.obj;
iv.setImageBitmap(bt);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.iv);
bt=(Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
new Thread(new Runnable()
{
@Override
public void run() {
String path="http://www.sinaimg.cn/dy/slidenews/1_img/2016_32/2841_720360_233449.jpg";
try {
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
InputStream is=conn.getInputStream();
Bitmap b=BitmapFactory.decodeStream(is);
Message msg=new Message();
msg.obj=b;
msg.what=1;
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
这是xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent" />