SAX 解析器不从 HttpsURLConection.getInputStream() 读取流
我的 Android 学习进度中的另一个小障碍。
这是我的代码:
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
byte[] encodedPassword = (user + ":" + pass).getBytes();
String auth = "Basic " + Base64.encodeToString(encodedPassword, false);
con.setRequestProperty("Authorization", auth);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type","text/xml");
con.setRequestProperty("Content-Length","" + Integer.toString(urlParameters.getBytes().length));
con.setRequestProperty("Content-Language", "en-US");
con.setRequestProperty("Connection", "close");
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
con.setAllowUserInteraction(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int statusCode = ((HttpURLConnection) con).getResponseCode();
Log.d(TAG, "Response Code = " + statusCode + " Content-Length = " + con.getContentLength());
我得到一个响应代码= 200,内容长度= 2593,所以我知道我可以访问该文件,
DataInputStream re = new DataInputStream(con.getInputStream());
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLmyHandler myHandler = new XMLmyHandler();
xr.setContentHandler(myHandler);
xr.parse(new InputSource(re));
该文件格式良好,我将其复制到本地非安全http服务器,它运行良好。
可悲的是,当我尝试从安全 http 执行相同操作时,它不起作用。
另外,通过我的非安全 http 成功尝试,我使用 HttpClient 来获取流,而不是此方法。
然而,我尝试将 HttpClient 与安全 http 结合使用却惨遭失败。
我更愿意保留这个方法,如果你知道有什么方法可以从我的“con”中提取与 SAX 一起使用的流,请告诉我!提前感谢我得到的任何帮助。
yet another tiny roadblock in my Android learning progress.
here's my code:
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
byte[] encodedPassword = (user + ":" + pass).getBytes();
String auth = "Basic " + Base64.encodeToString(encodedPassword, false);
con.setRequestProperty("Authorization", auth);
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type","text/xml");
con.setRequestProperty("Content-Length","" + Integer.toString(urlParameters.getBytes().length));
con.setRequestProperty("Content-Language", "en-US");
con.setRequestProperty("Connection", "close");
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
con.setAllowUserInteraction(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int statusCode = ((HttpURLConnection) con).getResponseCode();
Log.d(TAG, "Response Code = " + statusCode + " Content-Length = " + con.getContentLength());
I got a response code = 200 and content length = 2593 so i know i have access to the file
DataInputStream re = new DataInputStream(con.getInputStream());
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLmyHandler myHandler = new XMLmyHandler();
xr.setContentHandler(myHandler);
xr.parse(new InputSource(re));
the file is well formatted, i copied it to a local non secure http server and it worked perfectly.
Sadly, when i try to do the same from secure http it wouldn't work.
also, with my non-secure http successful attempts i use HttpClient to get a stream and not this method.
however, my attempts of using HttpClient with secure http failed miserably.
I'd prefer to keep this method, if you know any way to extract a stream from my "con" that works with SAX please let me know!!! thanks ahead on any help i get.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过反复试验,我发现了这个问题的肮脏修复,
我删除了数据输出流,然后数据输入流工作正常
after trial and error i found a dirty fix for this problem
i removed the data output stream then the data input stream worked fine
这里是可用的完整代码,请查看 Android XML 解析教程 - 使用 SAXParser
快乐编码:): ) :普拉格纳
here is compete code available have a look Android XML Parsing Tutorial - Using SAXParser
Happy coding :):) :Pragna
使用以下代码,
Use following code,