我正在创建一个应该从服务器获取数据并显示它的应用程序。当我提示用户登录时,如果他们输入主机名(例如stackoverflow.com),程序将崩溃,而如果他们输入IP(并且能够从站点获取数据),它将正常工作,例如198.252 .206.16(Stack Overflow的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" />
所以这不是问题。
logcat的:
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>
答案 0 :(得分:0)
我自己解决了这个问题(此处将评论重新发布给后人):
嗯,就我而言,结果有点奇怪。输入的主机名为server/
,导致其崩溃。该域名可通过浏览器访问。但是,使用server.companyname.com
(网址的“完整”版本)解决了问题。