我正在尝试将字符串从一个应用程序发送到另一个应用程序。我将返回一个字符串的响应。在这里,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)
答案 0 :(得分:12)
此代码无法将其识别为有效URI
,因为它缺少http
。这是我为解析代码而添加的内容:
builder.setScheme("http");