网页源码查看器 public class MainActivity extends Activity { protected static final int REQUESTSUCCESS = 0; protected stat
public class MainActivity extends Activity {
protected static final int REQUESTSUCCESS = 0;
protected static final int REQUESTFAIL = 1;
protected static final int REQUESTEXPECTioN = 2;
private EditText et;
private TextView tv;
private Handler handler = new Handler() {
public void handleMessage(Android.os.Message msg) {
switch (msg.what) {
case REQUESTSUCCESS:
String content = (String) msg.obj;
tv.setText(content);
break;
case REQUESTFAIL:
Toast.makeText(getApplicationContext(), "请求自资源不存在", 1).show();
break;
case REQUESTEXPECTION:
Toast.makeText(getApplicationContext(), "服务器忙,请稍后", 1).show();
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = findViewById(R.id.et);
tv = findViewById(R.id.tv);
}
public void click(View v) {
new Thread() {
public void run() {
try {
String path = et.getText().toString().trim();
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int code = connection.getResponseCode();
if (code == 200) {
InputStream in = connection.getInputStream();
String content = Change.convert(in);
Message msg = new Message();
msg.what = REQUESTSUCCESS;
msg.obj = content;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = REQUESTFAIL;
handler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
Message msg = new Message();
msg.what = REQUESTEXPECTION;
handler.sendMessage(msg);
}
}
}.start();
}
}
public class Change {
public static String convert(InputStream in) throws Exception {
ByteArrayOutputStream b = new ByteArrayOutputStream();
int length = -1;
byte[] buf = new byte[1024];
while ((length = in.read(buf)) != -1) {
b.write(buf, 0, length);
}
in.close();
String content = new String(b.toByteArray());
return content;
}
}
图片查看器(1)Bitmap cachebitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
Message msg = Message.obtain();
msg.obj = cachebitmap;
handler.sendMessage(msg);
图片查看器(2)
final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
runOnUiThread(new Runnable() {
public void run() {
iv.setImageBitmap(bitmap);
}
});
新闻客户端
public class MainActivity extends Activity {
private ListView lv;
private List list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = findViewById(R.id.lv);
getdate();
}
public void getdate() {
new Thread() {
public void run() {
try {
String path = “http://192.168.0.105:8080/a.xml”;
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(“GET”);
con.setConnectTimeout(5000);
int code = con.getResponseCode();
if (code == 200) {
InputStream in = con.getInputStream();
list = News.xmlpar(in);
runOnUiThread(new Runnable() {
public void run() {
lv.setAdapter(new MyAdapter());
}
});
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
private class MyAdapter extends BaseAdapter {
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = View.inflate(getApplicationContext(), R.layout.a, null);
} else {
view = convertView;
}
SmartImageView iv = view.findViewById(R.id.iv);
TextView tv1 = view.findViewById(R.id.tv1);
TextView tv2 = view.findViewById(R.id.tv2);
TextView tv3 = view.findViewById(R.id.tv3);
String image = list.get(position).getImige();
iv.setImageUrl(image);
tv1.setText(list.get(position).getTitle());
tv2.setText(list.get(position).getDescription());
String str = list.get(position).getType();
int type = Integer.parseInt(str);
switch (type) {
case 1:
tv3.setText(“国际”);
break;
case 2:
tv3.setText(“国内”);
break;
}
return view;
}
}
}
public class News {
private String title;
private String description;
private String imige;
private String type;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImige() {
return imige;
}
public void setImige(String imige) {
this.imige = imige;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public static List xmlpar(InputStream in) throws Exception {
List list = null;
News n = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, “utf-8”);
int tp = parser.getEventType();
while (tp != XmlPullParser.END_DOCUMENT) {
switch (tp) {
case XmlPullParser.START_TAG:
if (“channel”.equals(parser.getName())) {
list = new ArrayList();
} else if (“item”.equals(parser.getName())) {
n = new News();
} else if (“title”.equals(parser.getName())) {
n.setTitle(parser.nextText());
} else if (“description”.equals(parser.getName())) {
n.setDescription(parser.nextText());
} else if (“imige”.equals(parser.getName())) {
n.setImige(parser.nextText());
} else if (“type”.equals(parser.getName())) {
n.setType(parser.nextText());
}
break;
case XmlPullParser.END_TAG:
if (“item”.equals(parser.getName())) {
list.add(n);
}
break;
}
tp = parser.next();
}
return list;
}
}
--结束END--
本文标题: Android基础day4
本文链接: https://lsjlt.com/news/28877.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0