[android-developers] Can't access to a HTTPS
Hello,
I want to get information via HTTPS on the next site
https://b840b937347535c0bb8a81e134cf294349aa2852:x@nrikediaz.highrisehq.com/people.xml
but i can't get the xml data. I'm new onto HTTPS connections, but i
have a snippet of code about it, and when i run the Application i get
the next error: Error Null
Here's the Code from my class ParsingXML.java
package com.purplecat.hr;
import java.io.InputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ParsingXML extends Activity {
private static final String MY_DEBUG_TAG = "WeatherForcaster";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a new TextView to display the parsingresult later.
*/
TextView tv = new TextView(this);
try{
/* Create a URL we want to load some xml-data from. */
URL url = new URL("https://
b840b937347535c0bb8a81e134cf294349aa2852:x@nrikediaz.highrisehq.com/
people.xml");
HttpsURLConnection httpsConnection = null;
httpsConnection.setHostnameVerifier(new
AllowAllHostnameVerifier());
httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.connect();
InputStream inputStream = httpsConnection.getInputStream
();
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-
Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(inputStream));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us.
*/
ParsedExampleDataSet parsedExampleDataSet
=myExampleHandler.getParsedData();
/* Set the result to be displayed in our GUI. */
tv.setText(parsedExampleDataSet.toString());
} catch (Exception e){
/* Display any Error to the GUI. */
tv.setText("Error "+e.getMessage());
Log.e(MY_DEBUG_TAG, "WeatherQueryError", e); }
/* Display the TextView. */
this.setContentView(tv);
}
}
From ExampleHandler.java
package com.purplecat.hr;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ExampleHandler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private boolean in_outertag = false;
private boolean in_innertag = false;
private boolean in_mytag = false;
private ParsedExampleDataSet myParsedExampleDataSet = new
ParsedExampleDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("people")) {
this.in_outertag = true;
}else if (localName.equals("person")) {
this.in_innertag = true;
}else if (localName.equals("first-name")) {
this.in_mytag = true;
}else if (localName.equals("tagwithnumber")) {
// Extract an Attribute
String attrValue = atts.getValue("thenumber");
int i = Integer.parseInt(attrValue);
myParsedExampleDataSet.setExtractedInt(i);
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName,
String qName)
throws SAXException {
if (localName.equals("outertag")) {
this.in_outertag = false;
}else if (localName.equals("innertag")) {
this.in_innertag = false;
}else if (localName.equals("mytag")) {
this.in_mytag = false;
}else if (localName.equals("tagwithnumber")) {
// Nothing to do here
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.in_mytag){
myParsedExampleDataSet.setExtractedString(new String(ch,
start, length));
}
}
}
And from ParsedExampleDataSet.java
package com.purplecat.hr;
public class ParsedExampleDataSet {
private String extractedString = null;
private int extractedInt = 0;
public String getExtractedString() {
return extractedString;
}
public void setExtractedString(String extractedString) {
this.extractedString = extractedString;
}
public int getExtractedInt() {
return extractedInt;
}
public void setExtractedInt(int extractedInt) {
this.extractedInt = extractedInt;
}
public String toString(){
return "ExtractedString = " + this.extractedString
+ "\nExtractedInt = " + this.extractedInt;
}
}
Best Regards,
Enrique Díaz.
PurpleCat Project
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home