如何避免Android异步任务中的连接错误

时间:2014-05-04 15:54:34

标签: android android-asynctask timeout httpclient connection-timeout

在我的Android应用程序中,我调用一个php脚本,输出xml以在应用程序中使用。从分析中我发现,由于以下错误,某些用户无法获取数据:

libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)


java.net.ConnectException: failed to connect to /81.19.145.162 (port 80): connect failed: EHOSTUNREACH (No route to host)


libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)


libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)


java.net.ConnectException: failed to connect to /81.19.145.162 (port 80): connect failed: ECONNREFUSED (Connection refused)

异步任务的代码如下所示:

        try {
            HttpClient httpclient = new DefaultHttpClient();
            fullUrl = new StringBuilder(url);
            fullUrl.append("&lat=" + String.valueOf(lat) + "&lng=" + String.valueOf(lng) + "&token=" + token + "&types=" + types + "&limit=" + limit);
            HttpPost httppost = new HttpPost(fullUrl.toString());
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            InputStreamReader reader = new InputStreamReader(is);

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput(reader);
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {

                if (eventType == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("GeocacheTypeId")) {
                        typeSet.add(xpp.nextText().toString());
                    } else if (xpp.getName().equals("Code")) {
                        gcSet.add(xpp.nextText().toString());
                    } else if (xpp.getName().equals("Name")) {
                        nameSet.add(xpp.nextText().toString());
                    } else if (xpp.getName().equals("Latitude")) {
                        latSet.add(xpp.nextText().toString());
                    } else if (xpp.getName().equals("Longitude")) {
                        lngSet.add(xpp.nextText().toString());
                    } else if (xpp.getName().equals("HasbeenFoundbyUser")) {
                        foundSet.add(xpp.nextText().toString());
                    }

                }
                eventType = xpp.next();
            }
        } catch (Exception e) {
            showError(context.getResources().getString(R.string.xmlerror), false);
            sendToAnalytics();
        }

被叫网址如下所示:

http://mywebsite.at/folder/getcaches.php?getcaches&lat=37.9249409&lng=-92.5329998&token=%xxxx%2BVIMwAcVFvV0PCEQWCsb12I%3D&hidefounds=true&username=someuser&types=2%2C3%2C8%2C9%2C12%2C1304%2C3773%2C137%2C6%2C13%2C453%2C3653%2C3774%2C4738%2C4%2C1858%2C5%2C11%2C&limit=1

对我来说,一切都很完美,我无法重现错误。我认为不同国家的不同移动服务提供商存在问题。

我希望你能告诉我,我怎么能避免这些错误。

编辑 - 清单:

 <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package"
    android:versionCode="16"
    android:versionName="3.02" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="com.android.vending.BILLING" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="my.package.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="my.package.OAuth"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Transparent" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="myscheme" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
           android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

        <receiver
            android:name="my.package.WidgetProvider"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider" />
        </receiver>
        <receiver android:name="my.package.UpdateReceiver" />
        <receiver android:name="my.package.H24Receiver" />
        <receiver android:name="my.package.BootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name="com.google.analytics.tracking.android.CampaignTrackingService" />

        <receiver
            android:name="com.google.analytics.tracking.android.CampaignTrackingReceiver"
            android:exported="true" >
            <intent-filter>
                <action android:name="com.android.vending.INSTALL_REFERRER" />
            </intent-filter>
        </receiver>

        <service
            android:name="my.package.WidgetService"
            android:permission="android.permission.BIND_REMOTEVIEWS" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="mykey" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

  

libcore.io.ErrnoException:recvfrom失败:ETIMEDOUT(连接超时)

     

java.net.ConnectException:无法连接到/81.19.145.162(端口80):连接失败:EHOSTUNREACH(无主机路由)

     

libcore.io.ErrnoException:recvfrom failed:ECONNRESET(由对等方重置连接)

     

libcore.io.GaiException:getaddrinfo failed:EAI_NODATA(没有与主机名关联的地址)

这些都是用户端的连接问题。除了告诉用户检查网络连接并再试一次之外,您无法做任何事情。

  

java.net.ConnectException:无法连接到/81.19.145.162(端口80):连接失败:ECONNREFUSED(拒绝连接)

这意味着服务器可以访问,但端口中没有任何内容,即Web服务器已关闭。努力提高服务器的可靠性。对于最终用户,消息是存在临时问题,用户应稍后再试。