3

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

下面是我写的代码。

  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());  }

    }

代码一直运行到当我打印我看到的异常时 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)
4

1 回答 1

13

URI由于缺少此代码,因此无法将其识别为有效代码http。这是我为解决代码添加的内容:

builder.setScheme("http");
于 2013-08-06T21:24:09.863 回答