Link to home
Start Free TrialLog in
Avatar of isuhendro
isuhendroFlag for Singapore

asked on

HTTP Client in Apache return "Target host must not be null"

Dear experts,
I am using Apache HTTP client library for Android emulator.
http://hc.apache.org/httpcomponents-client/httpclient/apidocs/org/apache/http/client/package-summary.html

However whenever I enter e.g "www.google.com" or "http://www.google.com", it always return error "Target host must not be null"
But when i convert into IP Address, all works well.
Would you be able to advice?

Thanks!
public void run() {
			Log.i("GList", "downloadProcess run()");
			props = new Properties();
 
			try {
				String url;
				url = "http://www.google.com"; //THIS IS FAILED
				//url = "http://216.239.61.104"; // THIS IS OK
				HttpManager.doGet(url, props);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// TODO Auto-generated method stub
		}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>HttpManager.doGet(url, props);

What's that? It's not Apache HttpClient..
Avatar of isuhendro

ASKER

Hi CEHJ,
HttpManager just a wrapper of Apache HTTP client, please refer to the static method inside HttpManager below.
	public static String doGet (String serviceEndpoint, Properties props) throws Exception
	{
		HttpClient httpClient = new DefaultHttpClient();
		StringBuilder uriBuilder = new StringBuilder(serviceEndpoint);
		StringBuffer sbResponse = new StringBuffer ();		
		Enumeration<Object> enumProps = props.keys();
		String key, value = null;
		
		if (enumProps.hasMoreElements()) uriBuilder.append('?');
		while (enumProps.hasMoreElements())
		{
			key = (String)enumProps.nextElement();
			value = (String)props.get(key);
			uriBuilder.append(key);
			uriBuilder.append('=');
			uriBuilder.append(java.net.URLEncoder.encode(value));
			uriBuilder.append('&');			
		}
		HttpGet request = new HttpGet(uriBuilder.toString());
		
	HttpResponse response = httpClient.execute(request);
 
		

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi CEHJ,

thank you for your suggestion.
somehow this line java.net.URLEncoder gave invalid form URL
:-)