大家好我在这里发帖我的问题bcz我已经完成了所有帖子,但没有得到任何帮助我的问题。我正在使用泽西网络服务,并尝试通过URL访问Android手机。我想从网络服务打印问候消息,但它总是抛出java.lang.IllegalArgumentException Host name may not be null
,添加了互联网许可,
请检查我的活动
公共类MainActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
EditText content = (EditText)this.findViewById(R.id.editText1);
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpGet httpget = new HttpGet("http://localhost:10.0.2.2:8009/BookService/rest/bookresource/hello");
HttpResponse res;
try {
res = httpClient.execute(httpget);
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
br = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
while ((line = br.readLine()) != null) {
sb.append(line);
}
content.setText(sb);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这里是logcat输出
05-10 01:38:24.768: D/AndroidRuntime(1438): Shutting down VM
05-10 01:38:24.768: W/dalvikvm(1438): threadid=1: thread exiting with uncaught exception (group=0xb4b0cba8)
05-10 01:38:24.818: E/AndroidRuntime(1438): FATAL EXCEPTION: main
05-10 01:38:24.818: E/AndroidRuntime(1438): Process: com.example.androidhttp, PID: 1438
05-10 01:38:24.818: E/AndroidRuntime(1438): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhttp/com.example.androidhttp.MainActivity}: **java.lang.IllegalArgumentException: Host name may not be null**
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.os.Handler.dispatchMessage(Handler.java:102)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.os.Looper.loop(Looper.java:136)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-10 01:38:24.818: E/AndroidRuntime(1438): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 01:38:24.818: E/AndroidRuntime(1438): at java.lang.reflect.Method.invoke(Method.java:515)
05-10 01:38:24.818: E/AndroidRuntime(1438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-10 01:38:24.818: E/AndroidRuntime(1438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-10 01:38:24.818: E/AndroidRuntime(1438): at dalvik.system.NativeStart.main(Native Method)
05-10 01:38:24.818: E/AndroidRuntime(1438): Caused by: java.lang.IllegalArgumentException: Host name may not be null
05-10 01:38:24.818: E/AndroidRuntime(1438): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
05-10 01:38:24.818: E/AndroidRuntime(1438): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
05-10 01:38:24.818: E/AndroidRuntime(1438): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-10 01:38:24.818: E/AndroidRuntime(1438): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-10 01:38:24.818: E/AndroidRuntime(1438): at com.example.androidhttp.MainActivity.onCreate(MainActivity.java:59)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.Activity.performCreate(Activity.java:5231)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-10 01:38:24.818: E/AndroidRuntime(1438): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-10 01:38:24.818: E/AndroidRuntime(1438): ... 11 more
答案 0 :(得分:3)
HttpGet httpget = new HttpGet("http://localhost:10.0.2.2:8009/BookService/rest/bookresource/hello");
您网址的主机为http://localhost:10.0.2.2:8009/
,因此无效..请将其更改为http://localhost:8009/
或http://10.0.2.2:8009/
,这应该可以解决问题。错误的URL可能会导致HttpGet中出现异常,它处理URL会将null返回给代码的其余部分
答案 1 :(得分:0)
此处您的网址是
HttpGet httpget = new HttpGet("http://localhost:10.0.2.2:8009/BookService/rest/bookresource/hello");
表示您正在使用本地Web服务。因此,您必须在Emulator
而不是android phone device
中运行您的应用。因此,您需要将其更改为http://localhost:8009/
或http://10.0.2.2:8009/