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"
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!