Skip to content

Commit 52c9369

Browse files
authored
Merge pull request #497 from erdongxin/master
#6 #59 第六次实验
2 parents 65b7bee + fc5ff50 commit 52c9369

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package edu.hzuapps.androidlabs.homeworks.net1414080903122;
2+
3+
4+
import android.app.Activity;
5+
import android.graphics.Bitmap;
6+
import android.graphics.BitmapFactory;
7+
import android.os.Bundle;
8+
import android.os.Handler;
9+
import android.os.Message;
10+
import android.text.TextUtils;
11+
import android.view.View;
12+
import android.widget.EditText;
13+
import android.widget.ImageView;
14+
import android.widget.Toast;
15+
16+
import java.io.InputStream;
17+
import java.net.HttpURLConnection;
18+
import java.net.URL;
19+
20+
import edu.hzuapps.androidlabs.R;
21+
22+
/**
23+
* Created by Administrator on 2017/6/20 0020.
24+
*/
25+
26+
public class Net1414080903122Network extends Activity {
27+
protected static final int CHANGE_UI=1;
28+
protected static final int ERROR=2;
29+
private EditText et_path;
30+
private ImageView iv;
31+
//主线线程创建信息处理器
32+
private Handler handler=new Handler(){
33+
public void handlerMessage(Message msg){
34+
if(msg.what==CHANGE_UI){
35+
Bitmap bitmap=(Bitmap) msg.obj;
36+
iv.setImageBitmap(bitmap);
37+
}else if(msg.what==ERROR){
38+
Toast.makeText(Net1414080903122Network.this,
39+
"显示图片错误",Toast.LENGTH_SHORT).show();
40+
}
41+
};
42+
};
43+
protected void onCreate(Bundle saveInstaneState){
44+
super.onCreate(saveInstaneState);
45+
setContentView(R.layout.activity_net1414080903203network);
46+
et_path=(EditText) findViewById(R.id.et_path);
47+
iv=(ImageView) findViewById(R.id.iv);
48+
}
49+
public void click(View view){
50+
final String path=et_path.getText().toString().trim();
51+
if(TextUtils.isEmpty(path)){
52+
Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show();
53+
}else{
54+
new Thread(){
55+
private HttpURLConnection conn;
56+
private Bitmap bitmap;
57+
public void run(){
58+
try{
59+
URL url=new URL(path);
60+
conn=(HttpURLConnection) url.openConnection();
61+
conn.setRequestMethod("GET");
62+
conn.setConnectTimeout(5000);
63+
conn.setRequestProperty("User-Agent",
64+
"Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;"+ "SV1;.NET4.0C;NET4.0E;.NET CLR 2.0.50727;"+".NET CLR 3.0.4506.2152;.NET CLR 3.5.30729;Shuame)");
65+
int code=conn.getResponseCode();
66+
if(code==200){
67+
InputStream is=conn.getInputStream();
68+
bitmap= BitmapFactory.decodeStream(is);
69+
Message msg=new Message();
70+
msg.what=CHANGE_UI;
71+
msg.obj=bitmap;
72+
handler.sendMessage(msg);
73+
}else {
74+
Message msg=new Message();
75+
msg.what=ERROR;
76+
handler.sendMessage(msg);
77+
}
78+
}catch (Exception e){
79+
e.printStackTrace();
80+
Message msg=new Message();
81+
msg.what=ERROR;
82+
handler.sendMessage(msg);
83+
}
84+
};
85+
86+
}.start();
87+
}
88+
}
89+
90+
}
Loading
48.5 KB
Loading
58.7 KB
Loading
Loading
16.3 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical"
7+
tools:context=".homeworks.net1414080903122.Net1414080903122Network">
8+
<ImageView
9+
android:layout_weight="1000"
10+
android:id="@+id/iv"
11+
android:layout_width="fill_parent"
12+
android:layout_height="fill_parent"/>
13+
14+
<EditText
15+
android:singleLine="true"
16+
android:id="@+id/et_path"
17+
android:layout_width="fill_parent"
18+
android:layout_height="wrap_content"
19+
android:hint="输入图片路径"/>
20+
21+
<Button
22+
android:onClick="click"
23+
android:layout_width="fill_parent"
24+
android:layout_height="wrap_content"
25+
android:text="浏览"/>
26+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<infos>
4+
<city id="1">
5+
<temp>20/30</temp>
6+
<weather>晴天多云</weather>
7+
<name>上海</name>
8+
<pm>80</pm>
9+
<wind>1级</wind>
10+
</city>
11+
<city id="2">
12+
<temp>26/32</temp>
13+
<weather>晴天</weather>
14+
<name>北京</name>
15+
<pm>988888</pm>
16+
<wind>3级</wind>
17+
</city>
18+
<city id="3">
19+
<temp>15/24</temp>
20+
<weather>多云</weather>
21+
<name>吉林</name>
22+
<pm>30</pm>
23+
<wind>5级</wind>
24+
</city>
25+
26+
</infos>
27+
</resources>
28+
29+
30+

0 commit comments

Comments
 (0)