求传智播客python基础20151228Android基础视频学习笔记

500 Internal Server Error
500 Internal Server Error
nginx/1.9.12Service.java源码:
package com.sinaapp.ssun.
import java.io.InputS
import java.io.OutputS
import java.util.*;
import org.xmlpull.v1.XmlPullP
import org.xmlpull.v1.XmlPullParserF
import org.xmlpull.v1.XmlS
import android.util.X
import com.sinaapp.ssun.domain.P
public class Service {
* 获取XML文件中的数据
* @param xml
* @throws Exception
public static List&Person& getPersons(InputStream xml) throws Exception {
List&Person& persons =
XmlPullParser parser = XmlPullParserFactory.newInstance()
.newPullParser();
// parser = Xml.newPullParser();
parser.setInput(xml, "UTF-8");
int event = parser.getEventType();
Person p =
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
persons = new ArrayList&Person&();
case XmlPullParser.START_TAG:
if("person".equals(parser.getName())){
p = new Person();
int id = Integer.parseInt(parser.getAttributeValue(0));
p.setId(id);
if("name".equals(parser.getName())){
String name = parser.nextText();
p.setName(name);
if("age".equals(parser.getName())){
int age = Integer.parseInt(parser.nextText());
p.setAge(age);
case XmlPullParser.END_TAG:
if("person".equals(parser.getName())){
persons.add(p);
event = parser.next();
* 保存数据到XML文件中
* @param persons
* @param out
* @throws Exception
public static void save(List&Person& persons , OutputStream out) throws Exception{
XmlSerializer serializer
Xml.newSerializer();
serializer.setOutput(out, "UTF-8");
serializer.startDocument("UTF-8", true);
serializer.startTag(null, "persons");
for(Person p: persons){
serializer.startTag(null, "person");
serializer.attribute(null, "person", p.getId()+"");
serializer.startTag(null, "name");
serializer.text(p.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(p.getAge()+"");
serializer.endTag(null, "age");
serializer.endTag(null, "person");
serializer.endTag(null, "persons");
serializer.endDocument();
out.flush();
out.close();
Person.java源码:
package com.sinaapp.ssun.
public class Person {
public String getName() {
public void setName(String name) {
this.name =
public int getAge() {
public void setAge(int age) {
this.age =
public int getId() {
public void setId(int id) {
public Person(String name, int age, int id) {
this.name =
this.age =
public Person() {
public String toString() {
return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
text.xml文件:
&!--test.xml--&
&?xml version="1.0" encoding="UTF-8"?&&!-- 开始文档语法 --&
&name&ssun&/name&
&age&19&/age&
&name&cobe&/name&
&age&24&/age&
&/persons&
单元测试TestService.java源码:
package com.sinaapp.ssun.
import java.io.F
import java.io.FileNotFoundE
import java.io.FileOutputS
import java.util.ArrayL
import java.util.L
import android.test.AndroidTestC
import android.util.L
import com.sinaapp.ssun.domain.P
import com.sinaapp.ssun.service.S
public class TestService extends AndroidTestCase {
private final String Tag = "Test";
public void testPersons() throws Exception{
List&Person& persons = Service.getPersons(this.getClass().getClassLoader().getResourceAsStream("test.xml"));
for(Person p : persons){
Log.i(Tag, p.toString());
public void testSave() throws Exception{
List&Person& persons = new ArrayList&Person&();
persons.add(new Person("www",19,23));
persons.add(new Person("hhh",19,3));
persons.add(new Person("qqq",19,24));
persons.add(new Person("ooo",19,25));
File file = new File(this.getContext().getFilesDir(),"test2.xml");
FileOutputStream out = new FileOutputStream(file);
Service.save(persons, out);
浏览: 13747 次
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!安全检查中...
请打开浏览器的javascript,然后刷新浏览器
vxia.net 浏览器安全检查中...
还剩 5 秒&博客分类:
1、Android异步操作
··之前说过用Thread和Handler可以再Android中实现异步处理,这里讨论的使用AsyncTask类来实现异步处理,相比之下,运用这种方法来完成异步操作更为简单。贴个地址:
1)AsyncTask类中有许多函数,在执行AsyncTask时对应的条用顺序为:
onPreExxcute()--&doInBackground()--&onPostExecute();
2)onRreExecute是来准备开启一个新线程的,它是在UI线程中完成的,因此可以修改布局。
3)doInBackground是你想要异步执行的内容,它不是在UI线程中,因此不可以修改布局。
4)onPostExecute是任务结束后执行的内容,它是在UI线程中执行,因此也可以修改布局。
5)如果想要在doInBackground运行过程中更新布局,可以再doInBackground中调用publishProgress函数,该函数调用时会执行onProgressUpdate函数,该函数可以修改UI布局。
6)上述几个函数实际上都带有参数,这些参数是定义AsyncTask子类时定义的。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.click).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new async().execute();//执行任务,execute里面实际上是第一个参数
class async extends AsyncTask&Void , Integer , Void& {//三个参数类型
protected Void doInBackground(Void... params) {//返回值是第三个参数
// TODO Auto-generated method stub
for(int i = 0; i&=50 ; i++){
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
publishProgress(i);//传入的参数是第二个参数
protected void onProgressUpdate(Integer... values) {
TextView text = (TextView)findViewById(R.id.click);
text.setText(""+values[0]);
super.onProgressUpdate(values);
2、Http基础操作
1)常用的发送Http请求的方式有两种,get和post
2)想要获得http返回的数据,首先要生成一个请求包。
3)生成一个客户端对象。
4)客户端执行请求包。
5)得到响应对象。
6)从响应对象获取返回数据。代码如下:(Mars老师代码)
requestButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//生成一个请求对象
HttpGet httpGet = new HttpGet("http://www.baidu.com");
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用Http客户端发送请求对象
InputStream inputStream =
httpResponse = httpClient.execute(httpGet);
//获取数据流,将数据流转为字符串
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while((line = reader.readLine()) != null){
result = result +
Toast.makeText(MainActivity.this, result, 2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
inputStream.close();
catch(Exception e){
e.printStackTrace();
最后再贴个地址...
3、ListView的使用方法
1)要实现ListView,一种是直接使用ListView创建,另一种是将Activity继承ListActivity,这里只谈第一种,第二种感觉实际用处不大。
2)首先需要创建一个包含ListView的布局文件。
3)然后需要在创建一个布局文件,这个布局文件实际上是用来显示ListView中每一个元素的布局。
4)然后需要在Activity中创建一个ArrayList,ArrayList中每个元素都是一个HashMap,HashMap中可以包含多个键值对。
5)创建一个SimpleAdapter对象,这个对象是用来将ListView中显示的内容和控件一一对应起来,最后调用ListView的setAdapter函数即可。
包含ListView 的布局文件:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" &
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/&
&/LinearLayout&
ListView中每个元素的布局:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" &
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
&/LinearLayout&
Activity中的代码:
ArrayList&HashMap&String, String&& arrayList = new
ArrayList&HashMap&String, String&&();//创建一个ArrayList对象
ListView list = (ListView)findViewById(R.id.list);
HashMap&String, String& hashMap1 = new HashMap&String, String&();
hashMap1.put("name", "zhangsan");
hashMap1.put("age", "10");
arrayList.add(hashMap1);
HashMap&String, String& hashMap2 = new HashMap&String, String&();
hashMap2.put("name", "wanger");
hashMap2.put("age", "20");
arrayList.add(hashMap2);
SimpleAdapter adapter = new SimpleAdapter(this,arrayList,R.layout.layout4list,new String[]{"name","age"},new int[]{R.id.text1,R.id.text2});//创建一个SimpleAdapter,这位ListView的产生带来的方便
list.setAdapter(adapter);
//android.widget.SimpleAdapter.SimpleAdapter(Context context, List&? extends Map&String, ?&& data, int resource, String[] from, int[] to)
需要注意的是Adapter的种类有很多,因此创建ListView的方法也有很多,这里只是介绍一种,这种方法比较全面。
QCheng5453
浏览: 8779 次
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 传智播客python基础 的文章

 

随机推荐