MalformedByteSequenceException Invalid byte 1 of 1-byte UTF-8 sequence



I am coding an xml parser class and when i run it sometimes it works fine but another time it doesnt work and throws this exception ; MalformedByteSequenceException Invalid byte 1 of 1-byte UTF-8 sequence Can anyone help me?Please! Here is my code;



package TRT;



import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Gundem {


public static void main(String[] args) {
// TODO Auto-generated method stub

Gundem gundem=new Gundem();
try {
URL url=new URL("http://ift.tt/1rOOC8o");
URLConnection connection=url.openConnection();

DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=builderFactory.newDocumentBuilder();
Document document=docBuilder.parse(connection.getInputStream());

Element element=document.getDocumentElement();

Node node=(Node)element.getChildNodes();
System.out.println(node.getNodeName());


NodeList nodeList=node.getChildNodes();
Node channelNode=(Node)nodeList.item(0);
System.out.println(channelNode.getNodeName());

NodeList childNodeListOfChannelNode=channelNode.getChildNodes();

for(int i=0;i<childNodeListOfChannelNode.getLength();i++){
Node childNodesOfChannelNode=(Node)childNodeListOfChannelNode.item(i);
System.out.println(childNodesOfChannelNode.getNodeName());

if(childNodesOfChannelNode.getNodeName().equals(Constants.ITEM)){
Item item=new Item();
NodeList itemList=childNodesOfChannelNode.getChildNodes();
for(int j=0;j<itemList.getLength();j++){
Node childNodeOfItem=itemList.item(j);
if(childNodeOfItem.getNodeName().equals(Constants.TITLE)){
item.setTitle(childNodeOfItem.getTextContent());
System.out.println(item.getTitle());
System.out.println(gundem.dumpingInputAsHex(item.getTitle()));
}
else if(childNodeOfItem.getNodeName().equals(Constants.DESCRIPTION)){
item.setDescription(childNodeOfItem.getTextContent());
System.out.println(item.getDescription());
}

}

}

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



System.exit(0); // this line is for solving that problem; JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2


}

public String dumpingInputAsHex(String input){
return String.format("%40x",new BigInteger(1,input.getBytes()));
}

}

No comments:

Post a Comment