UnknownHostException 或 IllegalStateException

发布于 2024-12-12 04:08:18 字数 2460 浏览 0 评论 0原文

我正在尝试使用以下代码从我的应用程序连接到服务器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无声静候 2024-12-19 04:08:18

您是否在AndroidManifest.xml中请求INTERNET权限?

Do you request the INTERNET permission in AndroidManifest.xml?

鲜血染红嫁衣 2024-12-19 04:08:18

你会改变下面的代码吗?

之前:

 HttpGet getRequest = new HttpGet(url); 

之后:

 HttpGet getRequest = new HttpGet(); 
 getRequest.setURI(new URI(url));

would you change the code below?

before:

 HttpGet getRequest = new HttpGet(url); 

after:

 HttpGet getRequest = new HttpGet(); 
 getRequest.setURI(new URI(url));
听不够的曲调 2024-12-19 04:08:18

我假设您没有使用 http://somedomain.eu/blabla。如果是这样,您可能需要对 URL 进行 URLEncoder.encode() 。

但是使用 URI(如 IvoryCirrus 所建议的那样)可能会有更好的运气,因为

URI uri = new URI(<your url>);
mRequest = new HttpGet(mUri);

我有代码使用它,并且在直接使用 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

URI uri = new URI(<your url>);
mRequest = new HttpGet(mUri);

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.

黑寡妇 2024-12-19 04:08:18

我自己发现了错误...

开发是使用 Eclipse 进行的,部分是在我工作的 PC 上(当时间允许时),部分是在家里的 iMac 上...似乎其中之一没有抱怨我的拼写错误:

<uses_persmisson> should be <uses-permisson> in the Manifest file...

谢谢大家那是你的精神力贡献的!

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:

<uses_persmisson> should be <uses-permisson> in the Manifest file...

Thank you all that contributed with your mind power!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文