[英]FileNotFoundException while getting the InputStream object from HttpURLConnection
[英]Getting UnknownLengthHttpInputStream while getting InputStream from HttpURLConnection in android
HttpURLConnection.getInputStream()给出UnknownLengthHttpInputStream,由于此Document解析引发SAX解析器异常。
以下是代码
try{
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream xml = connection.getInputStream();
System.out.println(connection.getResponseCode());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(connection.getInputStream());
doc.getDocumentElement().normalize();
}catch(Exception e){
e.printStackTrace();
}
任何人都知道UnknownLengthHttpInputStream的原因。 我只在android中遇到此错误,此代码在Java Project中完美运行。
以下是LogCat的例外情况:
08-08 11:07:40.490: W/System.err(1493): org.xml.sax.SAXParseException: Unexpected end of document
08-08 11:07:40.504: W/System.err(1493): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:129)
08-08 11:07:40.510: W/System.err(1493): at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:107)
08-08 11:07:40.510: W/System.err(1493): at com.example.testws.MainActivity.onCreate(MainActivity.java:59)
08-08 11:07:40.520: W/System.err(1493): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-08 11:07:40.520: W/System.err(1493): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-08 11:07:40.520: W/System.err(1493): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-08 11:07:40.520: W/System.err(1493): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-08 11:07:40.530: W/System.err(1493): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
提前致谢。
它可能是一个Http 1.0(旧服务器或配置错误)服务器或没有保持活动配置。 在这种情况下,在从服务器关闭连接时,流的长度是已知的。 尝试在请求标头中指定http1.1和keep-alive(一些谷歌搜索将有助于此)。 仅当在服务器响应中指定了内容长度属性时,才会提前知道流长度。
解决方法:将http流完全read()
入ByteBufferStream
(直到read()
返回-1
)。 然后将ByteBufferInputStream抛出到您的库中(现在知道长度)
您是否尝试过使用apache库? 我建议如下:
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
然后获取HttpEntity
本身的流。 Smth喜欢:
InputStream st = entity.getContent();
更多示例: http : //www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/
要处理响应使用这种方法,它将照顾所有!
public static ResponseHandler<String> getResponseHandlerInstance(final Handler handler) {
final ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response) {
Message message = handler.obtainMessage();
Bundle bundle = new Bundle();
StatusLine status = response.getStatusLine();
Log.d(CLASSTAG, " " + HTTPRequestHelper.CLASSTAG + " statusCode - " + status.getStatusCode());
Log.d(CLASSTAG, " " + HTTPRequestHelper.CLASSTAG + " statusReasonPhrase - " + status.getReasonPhrase());
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
try {
result = HTTPRequestHelper.inputStreamToString(entity.getContent());
bundle.putString("RESPONSE", result);
message.setData(bundle);
handler.sendMessage(message);
} catch (IOException e) {
Log.e(CLASSTAG, " " + HTTPRequestHelper.CLASSTAG, e);
bundle.putString("RESPONSE", "Error - " + e.getMessage());
message.setData(bundle);
handler.sendMessage(message);
}
} else {
Log.w(CLASSTAG, " " + HTTPRequestHelper.CLASSTAG + " empty response entity, HTTP error occurred");
bundle.putString("RESPONSE", "Error - " + response.getStatusLine().getReasonPhrase());
message.setData(bundle);
handler.sendMessage(message);
}
return result;
}
};
return responseHandler;
}
private static String inputStreamToString(final InputStream stream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
}
当SAX解析器无法获得InputStream
数据的长度时,问题出现了。 要解决此问题,请将从xml
( InputStream
)读取的内容保存到String变量中,并从DocumentBuilder
parse
方法的变量中创建一个InputStream
。 为此,请按如下方式修改代码:
try {
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/xml");
InputStream xml = connection.getInputStream();
DataInputStream dis = new DataInputStream(xml);
StringBuilder sb = new StringBuilder();
int asci = dis.read();
while (asci > 0) {
sb.append((char) asci);
asci = dis.read();
}
String inputXML = sb.toString();
String predefinedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <EmotionDB></EmotionDB>";
InputStream inputStream = new ByteArrayInputStream(inputXML.getBytes());//use predefinedXML.getBytes() instead of inputXML.getBytes() for sample test
System.out.println(connection.getResponseCode());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputStream);
doc.getDocumentElement().normalize();
} catch (Exception e) {
e.printStackTrace();
}
这里inputStream
将具有正确的内容长度,因为它是从具有固定字节数的ByteArrayInputStream
构建的。
一旦尝试就像这样
if(connection.getResponseCode() ==HttpURLConnection.HTTP_OK){
InputStream xml = connection.getInputStream();
..........
}else{
//problem in URI/connection .....
}
您需要在服务中正确关闭输出流以避免此异常。 如果您使用的是第三方库,请确保已设置响应标头
Content-Type
Content-Length
如果您使用的是java服务,则可以从方法获取内容长度
File.length()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.