1

我正在制作一个应该从服务器获取数据并显示它的应用程序。当我提示用户登录时,如果他们输入主机名(例如 stackoverflow.com),程序将崩溃,而如果他们输入 IP(并且能够从该站点获取数据),它将正常工作,例如 198.252 .206.16(堆栈溢出的 IP 地址)。

这是应用程序中断的一些代码:

private String buildURL(Param... params)
{
    String server = SessionInfo.getInstance().getServer();
    try{
        server = InetAddress.getByName(server).getHostAddress();
    } catch (UnknownHostException e1){
        Log.d("UNKNOWN HOST EXCEPTION", e1.toString());
        e1.printStackTrace();
    }
    String sessionId = SessionInfo.getInstance().getSessionId();
    String url = "http://" + server + "/" + SITE_ADDRESS + "/api/login";
    return url;
}

编辑:

我的 AndroidManifest.xml 确实包含

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

所以这不是问题。

日志猫:

07-23 17:48:37.799: W/System.err(1447): java.net.UnknownHostException: Unable to resolve host "SERVER": No address associated with hostname
07-23 17:48:37.799: W/System.err(1447):     at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
07-23 17:48:37.829: W/System.err(1447):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-23 17:48:37.839: W/System.err(1447):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-23 17:48:37.839: W/System.err(1447):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-23 17:48:37.849: W/System.err(1447):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-23 17:48:37.878: W/System.err(1447):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-23 17:48:37.878: W/System.err(1447):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-23 17:48:37.899: W/System.err(1447):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-23 17:48:37.899: W/System.err(1447):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-23 17:48:37.919: W/System.err(1447):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-23 17:48:37.919: W/System.err(1447):     at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:252)
07-23 17:48:37.939: W/System.err(1447):     at com.app.android.rest.APIBase.post(APIBase.java:185)
07-23 17:48:37.958: W/System.err(1447):     at com.app.android.rest.LoginAPI.doInBackground(LoginAPI.java:44)
07-23 17:48:37.978: W/System.err(1447):     at com.app.android.rest.LoginAPI.doInBackground(LoginAPI.java:1)
07-23 17:48:37.978: W/System.err(1447):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-23 17:48:37.998: W/System.err(1447):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-23 17:48:38.008: W/System.err(1447):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-23 17:48:38.028: W/System.err(1447):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-23 17:48:38.028: W/System.err(1447):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-23 17:48:38.048: W/System.err(1447):     at java.lang.Thread.run(Thread.java:856)
07-23 17:48:38.048: W/System.err(1447): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
07-23 17:48:38.088: W/System.err(1447):     at libcore.io.Posix.getaddrinfo(Native Method)
07-23 17:48:38.088: W/System.err(1447):     at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
07-23 17:48:38.099: W/System.err(1447):     at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
07-23 17:48:38.099: W/System.err(1447):     ... 19 more
07-23 17:48:38.128: D/AndroidRuntime(1447): Shutting down VM
07-23 17:48:38.128: W/dalvikvm(1447): threadid=1: thread exiting with uncaught exception (group=0x40a71930)

还有我的清单:

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

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

    <uses-sdk android:minSdkVersion="8" 
                  android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/app_thumbnail"
        android:label="app Mobile"
        android:theme="@style/Theme.Sherlock" >

        <activity
            android:screenOrientation="portrait"
            android:name="com.app.android.LoginScreenActivity"
            android:theme="@style/Theme.Sherlock.NoActionBar"
            android:label="Mobile App" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:screenOrientation="portrait"
            android:name="com.app.android.MonitorSettingsActivity"
            android:label="" />

        <activity
            android:screenOrientation="portrait"
            android:name="com.app.android.MainScreenActivity"
            android:label="" >
            <intent-filter>
                <action android:name="com.app.android.MAINSCREENACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>
4

1 回答 1

0

自己解决了(在此处重新发布评论以供后代使用):

好吧,就我而言,结果有点奇怪。输入的主机名是server/,导致它崩溃。该域可通过浏览器访问。但是,使用server.companyname.comURL 的“完整”版本解决了该问题。

于 2017-05-11T16:41:42.017 回答