文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android基础day4

2022-06-06 13:02

关注

网页源码查看器
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)
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
iv.setImageBitmap(bitmap);
};
};

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;
 }
}


作者:byzcy


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯