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

Kumar Chandra Vart picture Kumar Chandra Vart · May 10, 2014 · Viewed 28k times · Source

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

public class MainActivity extends 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

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

Answer

Gary Schreiner picture Gary Schreiner · May 10, 2014
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