-1

I tried to download XML by using the code below:

 @Override
 protected String doInBackground(String... params) {
      try {    
           URL url = new URL("http://xx.xx.xx.xx/1.xml");
           URLConnection ucon = url.openConnection();
           ucon.setRequestProperty("Accept", "application/xml");

           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);

           ByteArrayBuffer baf = new ByteArrayBuffer(50);
           int current = 0;
           while ((current = bis.read()) != -1) {
                baf.append((byte) current);
           }
           String str = new String(baf.toByteArray(), "UTF8");

           return str;
      } catch (MalformedURLException e) {
           Log.e(DEBUG_TAG, "6",e);
      } catch (IOException e) {
           Log.e(DEBUG_TAG, "7",e);
      }
      return "error";
 }

and I am getting the error:

12-12 08:12:15.434: ERROR/myLogs(10977): java.io.FileNotFoundException: http://xx.xx.xx.xx/1.xml

If I open this url in browser is see:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Home>
<Child sex="male" age="5" name="Vasya"/>
<pets>
<Dog age="3" name="Druzshok"/>
</pets>
</Home>
Dante May Code
  • 11,177
  • 9
  • 49
  • 81
user1884872
  • 197
  • 1
  • 3
  • 14

3 Answers3

2

I guess your server intercept some request .

for example :

check [User-Agent] in headers.

ucon.setRequestProperty("Accept", "application/xml");  remove the line..
Cheung
  • 174
  • 5
0

You are wondering why the java URL object throws a file not found? It means the server responded to your request with a "404 not found" or for whatever reason no response was given by the server.

So you are wondering why that when you visit it in the browser it works fine, because the browser is treated differently by the server than your script. First try setting the user-agent to be the same as your browser. Servers are cracking down on robots more and more these days because of impolite script writers banging on their websites.

Source: java.io.FileNotFoundException for valid URL

Community
  • 1
  • 1
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
0

maybe server filter ['User-Agent']. code:

@Override
protected String doInBackground(String... params) {
    try {
        URL url = new URL("http://xx.xx.xx.xx/1.xml");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setReadTimeout(60 * 1000);
        conn.setConnectTimeout(30*1000);
        conn.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0");
        BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
        StringBuilder buf = new StringBuilder(512);
        String line = null;
        while((line=reader.readLine())!=null){
            buf.append(line).append("\n");
        }
        conn.disconnect();
        return buf.toString();
    }  catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Cheung
  • 174
  • 5