-1

以下のコードを使用して XML をダウンロードしようとしました。

 @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";
 }

エラーが発生しています:

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

この URL をブラウザで開くと、次のように表示されます。

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>
4

3 に答える 3

2

サーバーが何らかのリクエストを傍受していると思います。

例えば ​​:

ヘッダーの [User-Agent] を確認してください。

ucon.setRequestProperty("Accept", "application/xml");  remove the line..
于 2012-12-12T03:22:31.743 に答える
0

Java URL オブジェクトがファイルが見つからないことをスローするのはなぜでしょうか? これは、サーバーが「404 not found」でリクエストに応答したか、なんらかの理由でサーバーから応答がなかったことを意味します。

ブラウザはスクリプトとは異なる方法でサーバーによって処理されるため、ブラウザでアクセスすると正常に動作するのはなぜでしょうか。まず、ユーザーエージェントをブラウザと同じに設定してみてください。失礼なスクリプト ライターが Web サイトを攻撃しているため、最近、サーバーはますますロボットを取り締まっています。

ソース: 有効な URL の java.io.FileNotFoundException

于 2012-12-12T02:58:57.550 に答える