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

如何避免Android异步任务中的连接错误,android,android-asynctask,timeout,httpclient,connection-timeout,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: EHOSTUNR

在我的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
被调用的url如下所示:

        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>

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

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

libcore.io.ErrnoException:recvfrom失败:EconReset(由对等方重置连接)

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

这些都是用户端的连接问题。除了告诉用户检查网络连接并重试之外,您实际上什么也做不了

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


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

发布您的AndroidManifest.xml文件。我不认为这些只是用户的“仅仅”连接问题,因为我因为这些错误得到了很多糟糕的评论。从我的分析中,我发现有5-10%的接收错误。