HttpClient执行方法失败

HttpClient执行方法失败

我一定是做了些傻事,但我搞不懂是什么问题…
我的代码片段

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) {

在我的名单上我有上网许可
从控制台
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)

最佳答案

好吧,试试这个怎么样:

URL uri = new URL(rootUri);
HttpURLConnection connection = (HttpURLConnection) uri.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();

关于android - HttpClient执行方法失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7993773/

10-10 05:13