【问题标题】:HttpClient fails at execute methodHttpClient 在执行方法中失败
【发布时间】:2011-11-03 11:02:31
【问题描述】:

这一定是我正在做的一些愚蠢的事情,但我无法理解这是什么问题......

我的代码sn-p

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 是否真的存在 (http://somehost/WS2/Upload.aspx?one=valueGoesHere)?例如,尝试使用http://www.google.com,看看是否可行。
  • 这是实际的 URL 吗?如果 URL 有效并且您仍然遇到此问题,您应该阅读有关 DNS-prefetching
  • 好吧,也许它是,但我无法得到它,它不适用于 google.com 也没有

标签: android http httpclient httprequest httpresponse


【解决方案1】:

尝试在您的http请求代码前添加以下内容

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

【讨论】:

  • 这个解决方案对我有用,你能解释一下吗?
  • 它也对我有用。有人能解释一下为什么吗?
【解决方案2】:

不要忘记在 Android 清单中声明 INTERNET 权限

<uses-permission android:name="android.permission.INTERNET"/>  

【讨论】:

    【解决方案3】:

    你应该确保你没有在你的主线程上运行,检查你是否得到这个异常: android.os.NetworkOnMainThreadException

    【讨论】:

      【解决方案4】:

      好的,试试这个怎么样:

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

      【讨论】:

        【解决方案5】:

        你是在模拟器还是安卓设备上执行这个请求? 如果它在模拟器上,可能没有与它共享互联网连接。

        如果是在安卓设备上,似乎你没有互联网访问权限(不是权限,只是访问权限)

        【讨论】:

        • 嗯,我已连接到 Wifi,并且我有互联网连接,但只有我的应用程序无法运行 :(
        【解决方案6】:

        假设 URL 是有效的,我建议你应该调用:

        try {
            InetAddress address = InetAddress.getByName(url);
        
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        

        这是DNS pre-fetching,以便您的设备可以将其 URl 解析为 IP 地址。

        虽然这还不足以保证它工作。实际上对我有用的是重试机制。给它一些旧的 F5 F5 F5 F5 直到网站响应:D

        【讨论】:

        • tnx,对于 sn-p 但我不明白这对我的问题有何帮助?因为我在这一行有异常 client.execute(request) 我不知道为什么...
        • sn-p 实际上并没有多大帮助,你要做的就是实现一个重试机制。忽略异常在 while 循环中运行代码,直到获得 HTTP 200 响应。
        【解决方案7】:

        问题出在 URL 上。正如它在 Log Exception 中显示的那样

        java.net.UnknownHostException: somehost
        

        尝试使用您的真实网址。

        & BTW www.google.com 工作正常

        【讨论】:

        • 我没有,我也不知道为什么...W/System.err(4545): java.net.UnknownHostException: www.google.com W/System.err( 4545): 在 java.net.InetAddress.lookupHostByName(InetAddress.java:512) W/System.err(4545): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:300) W/System.err(4545) : 在 java.net.InetAddress.getAllByName(InetAddress.java:259)
        猜你喜欢
        • 1970-01-01
        • 2020-02-02
        • 2017-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 2023-03-15
        相关资源
        最近更新 更多