HttpURLConnection GET请求不返回任何内容

时间:2016-01-01 19:39:19

标签: android get httpurlconnection

我正在尝试执行GET请求,但即使日志中没有错误,请求也不会返回任何内容。

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Log.v("info", "get request starting"); //This message is showing in the console
                URL url = new URL("http://192.168.0.16:8888/piapi/index.php/piapi/getinfo");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
                InputStream stream = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line;
                Log.v("info", "Request executed"); //This is not showing
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line); // Doesn't show anything
                }
                bufferedReader.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();  
            } catch (IOException e) {    //No error in the log file
                e.printStackTrace();
            }
        }
    }).start();

我还尝试将它放在一个Asynctask类中,并且有几个URL,但这些选项都不起作用。有什么建议吗?感谢。

1 个答案:

答案 0 :(得分:0)

更新:事实证明代码工作正常,但Android Wear(我没有指定它,因为我认为它没有任何影响)不允许通过手表直接访问互联网,这就是它无法正常工作的原因。不管怎样,谢谢。