Java在新线程中与openConnection一起挂起

问题描述 投票:0回答:1

我正在构建一个应用程序,该应用程序在单独的线程中调用对REST API的API请求。调用后此应用仍挂起-未引发异常。

我尝试了许多方法来从我的Java应用程序发出GET请求,但都无法解决。

class ThreadRunner {

    public ThreadRunner() {
    }

    public static void main(String[] args) { 

        ThreadRunner threadRunner = new ThreadRunner();
        threadRunner.startChecking();

    }





    public void startChecking() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {

                APIUtils.sendGet("https://app01.mysimpledomain.abc/api/v2/user/1");
            }

        });

        thread.setName("ThreadRunner");
        //thread.setDaemon(true);
        thread.start();



    }

}
class APIUtils {

    public static void sendGet(String url) {


        try {
            System.out.println("Before invoking my url...");
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // optional default is GET
            con.setRequestMethod("GET");

            // add request header
            con.setRequestProperty("User-Agent", USER_AGENT);

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'GET' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            System.out.println( "response: " + response.toString() );
            System.out.println(response.toString());
        } catch (HttpStatusCodeException e) {
            System.out.println( "HttpStatusCodeException e: " + e.getMessage() );
        } catch (Exception e) {
            System.out.println( "Exception e: " + e.getMessage() );
//          LOG.error(e, e);
        }

    }
}

打印“连接之前...”后,消息停止。我应该看到我的API的输出。

我想说api调用可从chrome / firefox /邮递员获得。

编辑:

使用方法APIUtils.sendGet的URL:http://www.google.com/search?q=mkyong有效,但我的API URL之前挂起

Edit2:

我的API中的URL有效,并在Chrome / Firefox / Postman中提供了JSON。

java http
1个回答
0
投票
String url = "http://www.google.com/search?q=mkyong"; try { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); //add request header con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + url); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } catch (HttpStatusCodeException e) { attempts++; } catch (Exception e) { LOG.error(e, e); }
推荐问答
热门问答
最新问答