Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SAX解析器不';无法从HttpSurlConnection.getInputStream()读取流_Android_Xml_Https_Sax_Httpurlconnection - Fatal编程技术网

Android SAX解析器不';无法从HttpSurlConnection.getInputStream()读取流

Android SAX解析器不';无法从HttpSurlConnection.getInputStream()读取流,android,xml,https,sax,httpurlconnection,Android,Xml,Https,Sax,Httpurlconnection,在我的Android学习过程中还有一个小小的障碍 这是我的密码: HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); byte[] encodedPassword = (user + ":" + pass).getBytes(); String auth = "Basic " + Base64.encodeToString(encodedP

在我的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,请告诉我!!!感谢您对我的帮助。

经过反复试验,我找到了解决此问题的方法

我删除了数据输出流,然后数据输入流工作正常

        //DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        //wr.writeBytes(urlParameters);
        //wr.flush();
        //wr.close();
使用以下代码

SAXParserFactory spf = SAXParserFactory.newInstance();
  SAXParser sp = spf.newSAXParser();
  XMLReader xr = sp.getXMLReader();
  XMLmyHandler myHandler = new XMLmyHandler();
   xr.setContentHandler(myHandler);

            xr.parse(getInInputStreamFromURL(ur url here.....));


 public  AndroidHttpClient getClient(String userAgent) {
        HttpParams params = new BasicHttpParams();

        // Turn off stale checking. Our connections break all the time anyway,
        // and it's not worth it to pay the penalty of checking every time.
        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        // Default connection and socket timeout of 20 seconds. Tweak to taste.
        HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
        HttpConnectionParams.setSoTimeout(params, 20 * 1000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);

        // Don't handle redirects -- return them to the caller. Our code
        // often wants to re-POST after a redirect, which we must do ourselves.
        HttpClientParams.setRedirecting(params, false);

        // Set the specified user agent and register standard protocols.
        HttpProtocolParams.setUserAgent(params, userAgent);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

        HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
        schemeRegistry.register(new Scheme("https", socketFactory, 443));
        ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);

        // We use a factory method to modify superclass initialization
        // parameters without the funny call-a-static-method dance.
        return new AndroidHttpClient(manager, params);
    }


 public InputStream getInInputStreamFromURL(String url) {
        InputStream inputStream = null;
        AndroidHttpClient httpClient = null;
        try {
            httpClient = getClient("Ramindu");
            // Example send http request
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            inputStream = response.getEntity().getContent();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(TAG, "CAUGHT EXCEPTION : " + e);

        }
        return inputStream;
    }
下面是可用的代码,请看一看


快乐编码:)::Pragna

失败意味着什么?您能包括您获得的异常或您看到的错误吗?内容长度是否返回0?尝试使用https时的结果是什么?一个异常,一个不同的状态代码,来自服务器的错误输出?嗨,我没有得到任何异常。它只是没有执行任何操作,就像文件长度为0一样
try {
            StringBuffer inLine = new StringBuffer();
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            MyXMLHandler myExampleHandler = new MyXMLHandler();
            xr.setContentHandler(myExampleHandler);
            InputStream in = this.getResources().openRawResource(
                    R.raw.myxmlfile);
            xr.parse(new InputSource(in));
            MyXMLHandler parsedExampleDataSet = myExampleHandler;
            inLine.append(parsedExampleDataSet.toString());
            in.close();
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
            Log.i(TAG, e.toString());
        }