java.lang.IllegalArgumentException:具有未定义方案的 URI

Kri*_*jan 1 java

再会。我已经URIhttp调用定义了 2 。第一个效果很好,代码是除了一些参数之外的完整副本。第二个抛出一个Exception. 我将在下面列出代码。

public class WeatherAPI {
     
         private static String apiKey = "asdasd";
         private HttpResponse<String> response;
         private URI uri;
     
         private HttpRequest getRequest(URI newUri) {
             return HttpRequest.newBuilder()
                     .uri(newUri)
                     .build();
         }
     
         private HttpClient getClient() {
             return HttpClient.newBuilder().build();
         }
     
         private String getResponse(URI uri) {
             var client = getClient();
             var request = getRequest(uri);
             try {
                 response = client.send(request, HttpResponse.BodyHandlers.ofString());
             }  catch (IOException | InterruptedException e) {
                 e.printStackTrace();
             }
             return response.body();
         }
         public JSONObject getCityData(String lat, String lon) {
             String url = "api.openweathermap.org/data/2.5/weather"; //        System.out.println(uri);
             try {
                 uri = new URIBuilder(URI.create(url))
                         .addParameter("lat", lat)
                         .addParameter("lon", lon)
                         .addParameter("appid", apiKey)
                         .build();
                 System.out.println(uri);
     
             }  catch (URISyntaxException e) {
                 e.printStackTrace();
             }
             var body = getResponse(uri);
             System.out.println(body);
             return null;
         }
     
     }
Run Code Online (Sandbox Code Playgroud)

投掷

线程“main”中的异常 java.lang.IllegalArgumentException:具有未定义方案的 URI

Völ*_*raf 7

URL 似乎缺少协议前缀,例如http://https:// .. 尝试

http://api.openweathermap.org/data/2.5/weather

或者

https://api.openweathermap.org/data/2.5/weather

作为网址。这应该有效。