3

あるアプリケーションから別のアプリケーションに文字列を送信しようとしています。文字列である応答を返します。ここで、myhost.com/views は、文字列値を送信して応答を取得する必要がある 2 番目のアプリケーションです。しかし、送信しようとすると、このコードは実行されません。誰かが私が間違っているところを修正してもらえますか?

以下は私が書いたコードです。

  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