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
Lal
  • 14,726
  • 4
  • 45
  • 70

2 Answers2

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

Gary Schreiner
  • 902
  • 5
  • 14
  • 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?? – Kumar Chandra Vart May 10 '14 at 06:40
  • 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? – Gary Schreiner May 10 '14 at 06:46
  • 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 – Kumar Chandra Vart May 10 '14 at 07:20
  • 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. – Gary Schreiner May 10 '14 at 07:24
  • sorry, it;s not phone it's avd running on my machine and it's on which my server is running. – Kumar Chandra Vart May 10 '14 at 07:37
  • Ok, what happens if you navigate directly to the url from your web browser? – Gary Schreiner May 10 '14 at 07:46
  • it gives me message like "hello from web service". from browser only. same url i m using in my android activity. but no message at all – Kumar Chandra Vart May 10 '14 at 07:52
  • Put a breakpoint in your code and step through it to see what happens after you run `res = httpClient.execute(httpget);` – Gary Schreiner May 10 '14 at 07:54
  • thank you,got it , to use web service from emulator , i need to use "http://10.0.2.2:8009/ " but when i used "http://localhost:8009/" I was getting connection refused exception in my logcat, this i saw later, bcz it was not showing exception instantly. any how thanks for it. – Kumar Chandra Vart May 10 '14 at 10:10
  • Great, probably because your web server isn't bound to localhost interface, but to 10.0.2.2 .. – Gary Schreiner May 10 '14 at 10:12
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/

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • 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. – Kumar Chandra Vart May 10 '14 at 07:44