I really dont know why is this android code not working.. It shows unknown host exception:
String requestTpServer = "www.google.com";
HttpClient client = new DefaultHttpClient();
HttpResponse response = null ;
try {
HttpGet get = new HttpGet(requestToServer);
get.setHeader("Accept", "application/json");
get.setHeader("content-type", "application/json");
response = client.execute(getm);
Log.i("Hit Success... in get deals response", "going to rendernow");
if (response != null ) {
Log.i("responsecode---", ""+response.getStatusLine().getStatusCode());
String jsonResponseFromServer = response.toString();
Log.i("json Respon开发者_如何学编程se From Server", jsonResponseFromServer);
}
else {
Log.i("blank ", "reply ");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String res = response.toString();
return res;
Just for curiousity: did you try to prefix your URL with "http://"?
I'm using the following helper to open my HTTP connection:
private InputStream openHttpConnection() throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL("http://www.myserver.com/someurl");
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
Maybe that helps.
I got that exception when i was behind proxy...... So in case you are behind proxy, either contact admin or give proxy settings in code.
精彩评论