UrlConnection Android返回同一页面,其中包含2个不同的网址

时间:2013-03-11 15:41:44

标签: android urlconnection

这是代码:

    URL myUrl = new URL("http://www.militaryphotos.net/forums/showthread.php?224123-Weekend-Photos-9th-and-10th-March-2013/page1");
    URLConnection ucon = myUrl.openConnection();
    ucon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; pl; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2");
    InputStream is = ucon.getInputStream();   
    BufferedReader page = new BufferedReader(new InputStreamReader(is));     
    StringBuilder results = new StringBuilder();
    String linea="";  
    while((linea = page.readLine()) != null){       
        Log.w("myApp",linea);
        }
    is.close();
    page.close();

在此之后我启动 另一个 活动,需要下载 另一个 页面(类似于第一个,它仅在结尾处变化“2”)

http://www.militaryphotos.net/forums/showthread.php?224123-Weekend-Photos-9th-and-10th-March-2013/page2

如果我看到Logcat它会打印页面2次......

如果我尝试首先下载第2页然后第1页我们遇到同样的问题,但这次它会复制第2页。

如果我使用这段代码,

同样的事情:

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://www.militaryphotos.net/forums/showthread.php?224123-Weekend-Photos-9th-and-10th-March-2013/page2");
        HttpResponse response = httpClient.execute(httpGet, localContext);
        String result = "";
        BufferedReader page = new BufferedReader(
            new InputStreamReader(
              response.getEntity().getContent()
            )
          );

怎么可能?

----------------------------------------- --edit ---------------------------------------

我想我发现了这个问题。

当我抓取html时,我抓住了这个url militaryphotos.net/forums/showthread.php?t=224123,并成功地下载了第一页。

如果我使用militaryphotos.net/forums/showthread.php?t=224123/page2它会自动将我重定向到第1页。

在下面的代码中,我使用了另一个url格式militaryphotos.net/forums/showthread.php?224123-Weekend-Photos-9th-and-10th-March-2013/page1实际上有效,我将它与另一个混合这不起作用。

所以,如果我打扰你,我很抱歉。 :P

0 个答案:

没有答案