【发布时间】:2014-06-12 17:07:15
【问题描述】:
我的 android 应用程序有一个奇怪的问题。当我对服务器的 IP 地址进行硬编码时,我可以连接并发送数据,但是当我尝试使用主机名时出现以下错误。我一直在通过 Windows server 2003 上的 android 模拟器对此进行测试。我还确保地址以 http 开头,并且应用程序使用互联网的许可。关于为什么会这样的任何想法?例如,当我使用 123.45.678/test.php 但不是 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
发帖
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();
}
}
【问题讨论】:
-
你的服务器在运行吗?
-
@Rod_Algonquin 是的,我已经启用了 PHP,并且可以通过浏览器连接任何一种方式。
-
您是否尝试通过任何其他设备(例如 PC)连接到该主机?你的主机真的叫 HOSTNAME 吗?!
-
@nyarlathotep77 是的,我可以连接多台 PC。哈哈不,HOSTNAME 只是一个占位符。
-
能否查看您在 Android 应用中连接主机的方式?
标签: android android-emulator hostname windows-server