2

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 Response From Server", jsonResponseFromServer);
  }
  else {
   Log.i("blank ", "reply ");
  }
} catch (ClientProtocolException e) {

  e.printStackTrace();
} catch (IOException e) {

  e.printStackTrace();
}

String res = response.toString();
return res;

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.