我尝试下载xml文件以从服务器解析,但服务器不发送HTTP标头,只发送xml文本。我已经尝试了几乎所有使用我可以找到的URL下载文件的方法,但是一切都给了我一个I / O异常。我可以将文件加载到WebView中,但我无法自己下载文件。
如何打开连接并下载没有HTTP标头的xml文件?
编辑: 仍然给我一个I / O异常,这是我使用的代码:
private class UpdateTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void...voids ) {
String url = "http://192.168.1.92/state.xml";
try{
//get XML Pull parser
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
//factory.setValidating(false);
XmlPullParser xpp = factory.newPullParser();
HttpGet httpGet = new HttpGet(url);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpGet);
if(response == null) return null;
HttpEntity httpEntity = response.getEntity();
InputStream inStream = null;
StringBuilder sb = null;
if (httpEntity == null) return null;
try {
inStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inStream.close();
} catch (Exception ex) {
}
String xmlString = sb.toString();
xpp.setInput(new StringReader(xmlString));
int eventType = xpp.getEventType();
String name = null;
String text = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) { // <name>
name = xpp.getName();
System.out.println("got name: " + name);
} else if(eventType == XmlPullParser.END_TAG) {// </name> (xpp.getName();
} else if(eventType == XmlPullParser.TEXT) { // <>text</>
text = xpp.getText();
System.out.println("got text: " + text);
}
eventType = xpp.next();
}
finished = true;
}catch(MalformedURLException e){
System.out.println("Malformed URL");
}catch(IOException e){
System.out.println("IO Exception updating " + name + " at address: " + address);
}catch(XmlPullParserException e){
System.out.println("XML Pull Parser Error updating " + name + " at address: " + address);
}catch(Exception e){
}
return null;
}
}
每次给我一个I / O例外。但是,如果我将它指向提供HTTP标头的服务器,它下载就好了。服务器是我们在内部制作的设备,当您请求state.xml文件时,旧设备不提供HTTP标头,因此我需要能够同时使用这两种设备。
答案 0 :(得分:0)
查看文档如何正确配置连接,简要介绍一下这样的内容:
HttpGet httpGet = new HttpGet(mServerUrl);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpGet);
if(response == null) return;
HttpEntity httpEntity = response.getEntity();
InputStream inStream = null;
StringBuilder sb = null;
if (httpEntity == null) return;
try {
inStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inStream.close();
} catch (Exception ex) {
}
String xmlString = sb.toString();
答案 1 :(得分:0)
我必须打开自己的套接字,创建HTTP Get请求,然后删除有标题的服务器的HTTP标头。
public String getData(){
String returnData = "";
String requestmsg = "GET /state.xml HTTP/1.0\r\n";
requestmsg += "Connection: close\r\n";
if(cpPassword != null)
requestmsg += "Authorization: Basic " + Base64.encodeBytes(("admin:" + cpPassword).getBytes()) + "\r\n";
requestmsg += "\r\n";
int intPort = 80;
DataOutputStream dos = null;
BufferedReader dis = null;
Socket socket = null;
try {
socket = new Socket(address, intPort);
String data = "";
socket.setSoTimeout(3000); //timeout after 2 seconds
dos = new DataOutputStream(socket.getOutputStream());
dis = new BufferedReader(new InputStreamReader(socket.getInputStream()));
dos.write(requestmsg.getBytes());
StringBuilder sb = new StringBuilder();
while ((data = dis.readLine()) != null) {
sb.append(data);
}
returnData = sb.toString();
if(returnData.length() == 0)
return "";
int xmlIndex = returnData.indexOf( "<?xml" );
returnData = returnData.substring(xmlIndex);
return returnData;
} catch (IOException e) {
Log.e("ClientActivity", "C: Error Getting Data From Socket", e);
returnData = "";
}finally{
try{
if(socket!=null)
socket.close();
}catch(IOException e){
Log.e("ClientActivity", "C: Error Closing Socket", e);
}
}
return returnData;
}