Java 错误:目标主机不得为空,或在参数中设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18073249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:11:42  来源:igfitidea点击:

Error: Target host must not be null, or set in parameters

javahttpurl

提问by smiley

I am trying to send a string from one application to another. I would be returning a response which is a string. Here, myhost.com/views is the 2nd application where I need to send the string value and get a response from it. But when I am trying to send it is not executing this code. Can someone please correct me where I am wrong?

我正在尝试将一个字符串从一个应用程序发送到另一个应用程序。我将返回一个字符串响应。在这里,myhost.com/views 是我需要发送字符串值并从中获得响应的第二个应用程序。但是当我尝试发送时,它没有执行此代码。有人可以纠正我的错误吗?

Below is the code I have written.

下面是我写的代码。

  public static void sendData(String strval) throws IOException{ 
String doSend="https://myhost.com/views?strval="+strval;
   HttpClient httpclient = new DefaultHttpClient();
     try {
         System.out.println("inside try");
         URIBuilder builder = new URIBuilder();
         System.out.println("builder="+builder);

         builder.setHost("myhost.com").setPath("/views");
         builder.addParameter("strval", strval); 
         System.out.println("add param,sethost,setpath complete");

         URI uri = builder.build();
         System.out.println("uri="+uri);


         HttpGet httpget = new HttpGet(uri); 
         System.out.println("httpGet"+httpget);

         HttpResponse response = httpclient.execute(httpget);
         System.out.println(response.getStatusLine().toString());

         if (response.getStatusLine().getStatusCode() == 200) {
            String responseText = EntityUtils.toString(response.getEntity());
            System.out.println("responseText="+responseText);
            httpclient.getConnectionManager().shutdown();
         } else {
           System.out.println("Server returned HTTP code "
                    + response.getStatusLine().getStatusCode());
         }
      } catch (java.net.URISyntaxException bad) {
         System.out.println("URI construction error: " + bad.toString());
      }
      catch(Exception e){ System.out.println("e.getMessage=>"+e.getMessage());  }

    }

Code runs till and when I print the exceptions I see excep.getMessage() ->

代码运行直到我打印异常时我看到 excep.getMessage() ->

     java.lang.IllegalStateException: Target host must not be null, or set in parameters.
    at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:789)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:414)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

采纳答案by smiley

This code is not able to recognize it as a valid URIsince it was missing http. This is what I added to resolve the code:

此代码无法将其识别为有效,URI因为它丢失了http。这是我添加来解决代码的内容:

builder.setScheme("http");