2

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

2 Answers 2

4
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

Sign up to request clarification or add additional context in comments.

10 Comments

hey Gary , thanxs for reply,nd i changed it as you said ,great no error in logcat , but not getting any message from server, what could be possible reason??
did you change it to localhost or 10.0.2.2? also assuming there's no other errors, if you were to navigate directly to that URL on your phone what do you get?
it's localhost:8009/..., and blank text field shows up in emulator,this is what i want to fill with message by using setText. check my mainactivity that's all. No message from server
As mentioned in the other answer, you can't set it to localhost and expect it to run on your phone unless your web server you're pulling the result from is on that phone also. localhost is the local loop ip address of the device and never leaves the device itself. You have to change it to the ip address of the computer/server that holds the web service.
sorry, it;s not phone it's avd running on my machine and it's on which my server is running.
|
0

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 Emulator instead of android phone device. So you need to change it to either http://localhost:8009/, or http://10.0.2.2:8009/

1 Comment

Piyush , it;s not phone it's avd running on my machine and it's on which my server is running. i changed it to localhost:8009/ no error great !! but no output as well . blank textfield shows up in android emulator.pls help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.