Android java.lang.IllegalArgumentException:主机名不能为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23577528/
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-20 07:16:27  来源:igfitidea点击:

java.lang.IllegalArgumentException: Host name may not be null

androidandroidhttpclient

提问by user3622530

Hello everyone I'm posting here my question bcz i've gone through all the post , but didn't get any help for my problem. I'm using jersey web service and trying to access through android phone via URL .I want to print hello message from web service but it always throws java.lang.IllegalArgumentException Host name may not be null, added internet permission, please check my activity

大家好,我在这里发布了我的问题,因为我已经浏览了所有帖子,但没有为我的问题提供任何帮助。我正在使用 jersey Web 服务并尝试通过 URL 通过 android 手机访问。我想从 Web 服务打印 hello 消息,但它总是抛出 java.lang.IllegalArgumentException Host name may not be null,添加了互联网权限,请检查我的活动

public class MainActivity extends Activity {

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

}

}

here is logcat output

这是 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.access0(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

回答by Gary Schreiner

HttpGet httpget = new HttpGet("http://localhost:10.0.2.2:8009/BookService/rest/bookresource/hello");

the host of your URL is http://localhost:10.0.2.2:8009/and is therefore invalid .. change it to either http://localhost:8009/, or http://10.0.2.2:8009/and that should fix it. The bad URL is likely causing an Exception in the HttpGet where it processes the URL is returning a null to the rest of the code

您的 URL 的主机是http://localhost:10.0.2.2:8009/,因此无效.. 将其更改为http://localhost:8009/, orhttp://10.0.2.2:8009/并且应该修复它。错误的 URL 可能导致 HttpGet 中的异常,它在处理 URL 时向其余代码返回空值

回答by Piyush

Here your URL is

你的网址在这里

HttpGet httpget = new HttpGet("http://localhost:10.0.2.2:8009/BookService/rest/bookresource/hello");

Means you are using local web service. So you have to run your application in Emulatorinstead of android phone device. So you need to change it to either http://localhost:8009/, or http://10.0.2.2:8009/

表示您正在使用本地 Web 服务。所以你必须在Emulator而不是android phone device. 因此,您需要将其更改为http://localhost:8009/, 或http://10.0.2.2:8009/