I have a project where i download a XML file from a server using sockets, and doing a request with GET. This is the file:
http://api.yr.no/weatherapi/locationforecast/1.9/?lat=60.10;lon=9.58;msl=70
However when receiving the file it contains the string "008000" in some places of the file, which is not present when opened in my browser, and this string breaks the XML formating. Example:
<location altitude="70" latitude="6
008000
0.1000" longitude="9.5800">
I used wireshark to see if this was also sent from the server, or if it was created on my side, but it seems the server sends this:
Any ideas on how to fix this?
My code
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.*;
public class NetClient
{
Socket clientSocket = new Socket();
InetSocketAddress ip = new InetSocketAddress("api.yr.no", 80);
public String GetData(float _latitude, float _longitude, int _msl)
{
try
{
byte[] data = new byte[65000];
String translateddata = "";
clientSocket.connect(ip);
DataInputStream inData = new DataInputStream(clientSocket.getInputStream());
OutputStream outData = clientSocket.getOutputStream();
PrintWriter pw = new PrintWriter(outData, false);
pw.print("GET " + "/weatherapi/locationforecast/1.9/?lat=" + _latitude + ";lon=" + _longitude + ";msl=" + _msl + " HTTP/1.1\r\n");
pw.print("Host: api.yr.no\r\n");
pw.print("Accept: text/xml\r\n");
pw.print("\r\n");
pw.flush();
Thread.sleep(1000);
int bytesread = 0;
int i = 0;
while (bytesread != -1)
{
bytesread = inData.read(data);
if (bytesread != -1)
{
translateddata = translateddata + new String(data);
String temp = new String(data);
PrintWriter file;
file = new PrintWriter("weather" + i++ + ".txt");
file.write(temp);
file.close();
}
}
clientSocket.close();
return translateddata;
}
catch (IOException e)
{
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
}
return "Something went wrong when trying to download data from remote server!";
}
}