MalformedURLException报错!!
谁能贴一下HTTPthread的源码过来对比一下吗 感激不尽
谁能贴一下HTTPthread的源码过来对比一下吗 感激不尽
2016-02-25
package com.example.handexu.httpurlconnect;
import android.os.Handler;
import android.os.Message;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by handexu on 2/26/16.
*/
public class HttpXml extends Thread {
private String url;
private Handler handler;
private android.util.Log log;
public HttpXml(String url,Handler handler){
this.url = url;
this.handler = handler;
}
@Override
public void run() {
try {
URL httpUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
InputStream in = conn.getInputStream();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();//解析 xml文件
XmlPullParser parser = factory.newPullParser();
parser.setInput(in,"UTF-8");
int evenType = parser.getEventType(); //获取eventType进行判断
List<Girl> list =new ArrayList<Girl>();
Girl girl = null;
while (evenType != XmlPullParser.END_DOCUMENT){
String data = parser.getName();
switch (evenType){
case XmlPullParser.START_TAG:
if("girl".equals(data)){
girl = new Girl();
}
if("name".equals(data)){
girl.setName(parser.nextText());
}
if("age".equals(data)){
girl.setAge(Integer.parseInt(parser.nextText()));
}
if ("school".equals(data)){
girl.setSchool(parser.nextText());
}
break;
case XmlPullParser.END_TAG://进行结束符号
if("girl".equals(data) && girl != null){
list.add(girl); //添加到集合
}
break;
}
evenType = parser.next();
}
//handler发送消息
Message msg = new Message();
msg.what = 0x1233;
msg.obj = list.toString();
handler.sendMessage(msg);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}
举报