我正在升级到 Android 4.2 的三星 Galaxy S 上开发我的应用程序。应用程序在此设备上正常运行。
当我在运行 Android 2.2 的手机上对其进行测试时,会发生异常。
我反复检查,发现问题是在应用程序发送一个UDP DatagramPacket之后发生的。
当我评论有关发送 UDP DatagramPacket 的代码时,问题永远不会发生。
谁能告诉我这是什么原因?如何解决问题?
主要方法:</p>
public static String getDataFromServerInPostMethod(String url,
String content) {
HttpURLConnection httpurlconnection = null;
String result = "";
try {
InputStream stream = null;
// String host = android.net.Proxy.getDefaultHost();
// if (host != null) {
// int port = android.net.Proxy.getDefaultPort();
// SocketAddress vAddress = new InetSocketAddress(host, port);
// java.net.Proxy vProxy = new java.net.Proxy(
// java.net.Proxy.Type.HTTP, vAddress);
// httpurlconnection = (HttpURLConnection) new URL(url)
// .openConnection(vProxy);
// } else {
// httpurlconnection = (HttpURLConnection) new URL(url)
// .openConnection();
// }
httpurlconnection = (HttpURLConnection) new URL(url)
.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
// httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setReadTimeout(TIMEOUT * 1000);
httpurlconnection.setConnectTimeout(TIMEOUT * 1000);
try {
httpurlconnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
httpurlconnection.connect();
MyLog.e(TAG, "getURL:" + httpurlconnection.getURL());
httpurlconnection.getOutputStream().write(content.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
stream = httpurlconnection.getInputStream();
result = SystemUtil.convertStreamToString(stream);
if (httpurlconnection.getResponseCode() == 200) {
MyLog.w(TAG,
"Responsed-->>getURL:" + httpurlconnection.getURL());
} else {
MyLog.e(TAG,
"not Responsed,ResponseCode:"
+ httpurlconnection.getResponseCode());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
}
return result;
}