2012-05-31 57 views
1

在我的应用程序,我想打通过浏览器打开(计算机上以及时我用下面的代码Android的HTTP请求奇怪404没有找到问题

try { 
    url = new URL(serverURL); 

    httpURLConnection = (HttpURLConnection) url.openConnection(); 

    int timeout = 30000; 
    httpURLConnection.setConnectTimeout(timeout); 
    httpURLConnection.setReadTimeout(timeout); 

    httpURLConnection.connect(); 

    String httpResponseMessage = httpURLConnection.getResponseMessage(); 
    responseCode = httpURLConnection.getResponseCode(); 

    Log.i(LOG_TAG,"Response code "+responseCode); 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 

的(秘密)的URL网址如在电话中),完美地工作,并且响应如预期。但是当我通过上面的代码点击相同的URL时,它会给出响应代码404(NOT FOUND)。有人可以告诉我这个问题是什么吗? (对不起,不能发布网址,因为它是高度机密的。)

+0

您是否尝试过使用相同的代码不同的网址? –

+0

对于其他URL,响应代码为200。 – Rajkiran

+0

那么你能在手机的浏览器中打开该URL吗? (如果是防火墙配置问题) –

回答

1

问题解决了:)

 try { 

      url = new URL(serverURL); 

      Log.i(LOG_TAG, url+""); 
      HttpGet method= new HttpGet(new URI(serverURL)); 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(); 
      request.setURI(new URI(serverURL)); 
      HttpResponse response = client.execute(method); 
      responseCode = response.getStatusLine().getStatusCode(); 

      Log.i(LOG_TAG,"Response code response "+response); 
      Log.i(LOG_TAG,"Response responseCode "+responseCode); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
+0

您是否已经找出导致错误的原因? –

0

其实你甚至不需要按照你的两行代码。

HttpGet request = new HttpGet(); 
request.setURI(new URI(serverURL)); 

一个HttpGet就够了,你不需要它两次。

0

不知道这是否重要,但我有确切的问题。

我正在做一些明确地80端口的东西,并删除这条线使工作:

HttpHost host = new HttpHost(targetHost, 80, "http");