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



在我的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();
        }

被调用的url如下:

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

对我来说,一切都很完美,我不能重现错误。我认为不同国家的不同移动运营商存在不同的问题。

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

Edit - Manifest:

 <?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>

libcore.io。ErrnoException: recvfrom failed: ETIMEDOUT(连接超时)

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)

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

相关内容

  • 没有找到相关文章

最新更新