Search code examples
androidandroid-emulatorhostnamewindows-server

Android app can't connect to hostname but can connect to IP on server


I am having an odd problem with my android app. I can connect and send data when I hardcode my server's IP address, but I get the errors below when I try to use the hostname. I have been testing this through android's emulator on Windows server 2003. I also have made sure to ensure that the address starts with http and that the app uses the internet's permission. Any ideas as to why this could be? E.G I can connect when I use 123.45.678/test.php but not servername/test.php

06-12 17:02:30.360: I/Choreographer(642): Skipped 31 frames!  The application may be doing too much work on its main thread.
06-12 17:03:07.670: I/Choreographer(642): Skipped 85 frames!  The application may be doing too much work on its main thread.
06-12 17:03:07.810: I/Choreographer(642): Skipped 36 frames!  The application may be doing too much work on its main thread.
06-12 17:03:16.200: W/System.err(642): java.net.UnknownHostException: Unable to resolve host "HOSTNAME": No address associated with hostname

Post

public void postData(String Filename) {
                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                if (!MainActivity.address.startsWith("https://") && !MainActivity.address.startsWith("http://")){
                    MainActivity.address = "http://" + MainActivity.address;
                }
                HttpPost httppost = new HttpPost(MainActivity.address);
                HttpResponse response = null;
                try {
                    // Add your data
                    //MainActivity.QRdata.replaceAll("\\s+","");
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("data", "<data><serial><serial>SN001</serial></serial><items><item>Test1 = Failed</item><item>Test2 = Passed</item><item>Test3 = Passed</item></items></data>"));
                    nameValuePairs.add(new BasicNameValuePair("file", Filename));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    // Execute HTTP Post Request
                    response = httpclient.execute(httppost);
                    String res = EntityUtils.toString(response.getEntity());
                    Log.v("Response = ", res);

                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                    // TODO Auto-generated catch block
                } catch (IOException e) {
                    e.printStackTrace();
                    // TODO Auto-generated catch block
                }
                //return response.toString();
            }

        }

Solution

  • My problem was special permissions with my company that I did not understand. I was unable to use my app to connect due to security considerations. However, if anyone else runs into the same problem that I did (I just wanted to host a PHP script to use POST), use Google App Engine. I have been using it now and it is everything I have been looking for. https://appengine.google.com/