vendredi 6 mars 2015

XML Parsing error | OSM Script Android



After executing the code below I receive an error stating "Illegal character is query at index 95: http://ift.tt/1wclBZ6 (around:40,54.95092211711581,-7.721371648195827) ["highway"] ["maxspeed"]; ( ._; >; ); out;"


Could anyone have a look at this and offer any advice?


Thanks.


XML File I'm trying to parse:



<way id="64273241">
<nd ref="768053039"/>
<nd ref="1922602861"/>
<nd ref="1922622063"/>
<nd ref="795319854"/>
<nd ref="795320324"/>
<tag k="highway" v="secondary"/>
<tag k="maxspeed" v="60"/>
<tag k="name" v="Port Road"/>
<tag k="oneway" v="no"/>
<tag k="ref" v="R229"/>
</way>


MainActivity:



public class MainActivity extends ActionBarActivity {

TextView txtLat;
TextView txtLong;
TextView txtSpeed;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.latTxt);
txtLong = (TextView) findViewById(R.id.longTxt);
txtSpeed = (TextView) findViewById(R.id.carSpeedTxt);

LocationManager ls = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener li = new mylocationListener();
ls.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, li);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

//Location listener
class mylocationListener implements LocationListener
{

@Override
public void onLocationChanged(Location location) {
if(location != null)
{
//Get position
double lon = location.getLongitude();
double lat = location.getLatitude();
txtLong.setText("Longitude: " +lon);
txtLat.setText("Latitude: " + lat);

//Get speed
double speed = location.getSpeed();
txtSpeed.setText(speed + " km/h");

//Send request to recieve information regarding Speed zones everytime there is a change in location
//sendRequest(lat+"", lon+"");
//sendGetRequest(lat+"", lon+"");
readXML2(lat+"", lon+"");

}

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}


}

/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;

try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}

public void readXML2(String lat, String lon)
{
String query = "http://ift.tt/1wclBZa(around:40,"
+lon+","+lat+")" + " [\"highway\"] [\"maxspeed\"]; ( ._; >; ); out;";


try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document;
document = documentBuilder.parse(MainActivity.class.getResourceAsStream(getXmlFromUrl(query)));

NodeList nodeNodeList = document.getElementsByTagName("way");

for (int i = 0; i < nodeNodeList.getLength(); i++) {

Node nNode = nodeNodeList.item(i);

String limit = nNode.getAttributes().getNamedItem("maxspeed").getNodeValue();
TextView curLimitText = (TextView)findViewById(R.id.curLimitTxt);
curLimitText.setText(limit +" km/h");


System.out.println(nNode.getAttributes().getNamedItem("maxspeed").getNodeValue());
System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());

}

} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}



Aucun commentaire:

Enregistrer un commentaire