Reputation: 31963
It must be some idiotic thing that I am doing but I could not get it what is the problem...
My code snippet
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://somehost/WS2/Upload.aspx?one=valueGoesHere");
client.execute(request);//it fails at this line
} catch (Exception e) {
and in my manifest I have internet access permission
from console
W/System.err( 4210): java.net.UnknownHostException: somehost
W/System.err( 4210): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
W/System.err( 4210): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
W/System.err( 4210): at java.net.InetAddress.getAllByName(InetAddress.java:242)
W/System.err( 4210): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
W/System.err( 4210): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err( 4210): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err( 4210): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
W/System.err( 4210): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err( 4210): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err( 4210): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err( 4210): at com.temp.services.httpclient.HttpGetDemo.onCreate(HttpGetDemo.java:29)
W/System.err( 4210): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
W/System.err( 4210): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
W/System.err( 4210): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
W/System.err( 4210): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
W/System.err( 4210): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
W/System.err( 4210): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 4210): at android.os.Looper.loop(Looper.java:144)
W/System.err( 4210): at android.app.ActivityThread.main(ActivityThread.java:4937)
W/System.err( 4210): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 4210): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err( 4210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
W/System.err( 4210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
W/System.err( 4210): at dalvik.system.NativeStart.main(Native Method)
I/ActivityManager( 120): Displayed activity com.temp.services/.httpclient.HttpGetDemo: 119 ms (total 304 ms)
Upvotes: 1
Views: 11596
Reputation: 5017
Try to add the following before your http request code
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Upvotes: 4
Reputation: 213
You should make sure your are not running on your main thread, check if you get this exception: android.os.NetworkOnMainThreadException
Upvotes: 1
Reputation: 11
Don't forget to declare the INTERNET permission in the Android manifest:
<uses-permission android:name="android.permission.INTERNET"/>
Upvotes: 1
Reputation: 2696
ok, What about trying this :
URL uri = new URL(rootUri);
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
Upvotes: 0
Reputation: 11097
The Problem is with URL.As it shows in Log Exception is
java.net.UnknownHostException: somehost
Try with your true URL.
& BTW www.google.com works fine
Upvotes: 0
Reputation: 33792
Assuming that the URL is a valid one, I suggest that you should call :
try {
InetAddress address = InetAddress.getByName(url);
} catch (UnknownHostException e) {
e.printStackTrace();
}
This is DNS pre-fetching so that your device can resolve it URl to an IP address.
Although this isn't enough for it to guarantee to work. What actually worked for me is a retrying mechanism. Give it some good old F5 F5 F5 F5 till the website responds :D
Upvotes: 0
Reputation: 2696
Do you execute this request in the emulator or on an android device ? if it's on the emulator, maybe the internet connection is not shared with it.
If it's on an android device, seems you don't have internet access ( not the permission, just the access )
Upvotes: 0