UnknownHostException 或 IllegalStateException
我正在尝试使用以下代码从我的应用程序连接到服务器:
public void buildGamesList(View view){
String url = "http://somedomain.eu/blabla";
String response = "...";
try {
HttpGet getRequest = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(getRequest);
response = responseToString(resp);
} catch (ClientProtocolException e1) {
System.out.println("ClientProtocolException: " + e1.toString());
} catch (IOException e2) {
System.out.println("IOException: " + e2.toString());
}
System.out.println("Response: " + response);
}
单击定义如下的图像按钮时会调用此方法
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<ImageButton android:src="@drawable/logo"
android:id="@+id/logoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:onClick="buildGamesList"></ImageButton>
</LinearLayout>
,我遇到的问题是,如果我像上面那样编写 url(带有前导 http:// )我得到 UnknowHostException,如果我跳过前导 http://,我得到 java.lang.IllegalStateException:目标主机不能为空,或在参数中设置。
有人可以帮我理解这一点吗?
清单文件的一部分:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jef.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<permission android:name="android.permission.INTERNET"></permission>
<uses_permission android:name="android.permission.INTERNET"></uses_permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GamesListActivity"
android:permission="android.permission.INTERNET"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
I am trying to connect to a server from my app using the following code:
public void buildGamesList(View view){
String url = "http://somedomain.eu/blabla";
String response = "...";
try {
HttpGet getRequest = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(getRequest);
response = responseToString(resp);
} catch (ClientProtocolException e1) {
System.out.println("ClientProtocolException: " + e1.toString());
} catch (IOException e2) {
System.out.println("IOException: " + e2.toString());
}
System.out.println("Response: " + response);
}
This method is called when clicking on an image button defined like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<ImageButton android:src="@drawable/logo"
android:id="@+id/logoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_gravity="center"
android:onClick="buildGamesList"></ImageButton>
</LinearLayout>
and the problem I have is that if I write the url like above (with a leading http://) I get UnknowHostException, and if I skip the leading http:// I get an java.lang.IllegalStateException: Target host must not be null, or set in parameters.
Could someone please help me understand this?
Part of the Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jef.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<permission android:name="android.permission.INTERNET"></permission>
<uses_permission android:name="android.permission.INTERNET"></uses_permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GamesListActivity"
android:permission="android.permission.INTERNET"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否在AndroidManifest.xml中请求INTERNET权限?
Do you request the INTERNET permission in AndroidManifest.xml?
你会改变下面的代码吗?
之前:
之后:
would you change the code below?
before:
after:
我假设您没有使用 http://somedomain.eu/blabla。如果是这样,您可能需要对 URL 进行 URLEncoder.encode() 。
但是使用 URI(如 IvoryCirrus 所建议的那样)可能会有更好的运气,因为
我有代码使用它,并且在直接使用 url 字符串时似乎记得像您这样的问题。值得一试。请注意,在构建 URI 时,您仍然需要对 url 的非域部分进行编码 - 但这仅在您的 GET 到达服务器时才会出现问题。
I presume you are not using http://somedomain.eu/blabla. If so you may need to URLEncoder.encode() the URL.
But you may have better luck using a URI (as IvoryCirrus suggests) as in
I have code working using this and seem to remember problems like yours when using a url string directly. It's worth a try. Note that you still need to encode the non-domain part of the url when constructing the URI - but that will only be an issue when your GET gets to your server.
我自己发现了错误...
开发是使用 Eclipse 进行的,部分是在我工作的 PC 上(当时间允许时),部分是在家里的 iMac 上...似乎其中之一没有抱怨我的拼写错误:
谢谢大家那是你的精神力贡献的!
I found the error myself...
Development is made using Eclipse, partly on my PC at work when time admits, and partly at home on my iMac...it seems that one of them did not complain about my typo:
Thank you all that contributed with your mind power!