Reputation: 23
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"
Upvotes: 1
Views: 1310
Reputation: 23
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!
Upvotes: 0
Reputation: 4196
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.
Upvotes: 0
Reputation: 622
would you change the code below?
before:
HttpGet getRequest = new HttpGet(url);
after:
HttpGet getRequest = new HttpGet();
getRequest.setURI(new URI(url));
Upvotes: 0