1

在我的应用程序中,我正在尝试使用以下代码访问一个 URL

  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(未找到)。谁能告诉我可能是什么问题?(抱歉,由于高度机密,不能发布 URL。)

4

4 回答 4

1

你确定你在你的android.permission.INTERNET中声明了AndroidManifext.xml吗?

于 2012-05-31T14:45:23.057 回答
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();
        }
于 2012-07-19T15:41:20.100 回答
0

不确定这是否重要,但我遇到了确切的问题。

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

HttpHost host = new HttpHost(targetHost, 80, "http");
于 2015-01-21T21:21:30.433 回答
0

实际上,您甚至不需要在代码中遵循两行代码。

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

一个 HttpGet 就足够了,您不需要两次。

于 2013-05-11T08:41:41.823 回答