2012年11月25日 星期日

[RESOLVED] E/ERR(1183): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused

Today I am trying to write an android program to retrieve an XML data from localhost server. When running the program, the following error occurs:

E/ERR(1183): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused




              String url = "http://localhost:100/test.xml";

              try {
                     DefaultHttpClient httpClient = new DefaultHttpClient();
                     HttpPost httpPost = new HttpPost(url);

                     HttpResponse httpResponse = httpClient.execute(httpPost);
              } catch (ClientProtocolException e1) {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
              } catch (IOException e1) {
                     // TODO Auto-generated catch block
                     e1.printStackTrace();
              }
             
 


Solution:
The Android emulator could not recognize the localhost and results in connection refused.
To resolve, we can change the localhost to the ip address 10.0.2.2 and try again, problem resolved.

e.g. String url = "http://10.0.2.2:100/test.xml";
 



沒有留言:

張貼留言