本文正在参加「Java主题月 - Java Debug笔记活动」,详情查看<活动链接>
提问:可以设置哪些Java命令行选项以允许对JVM进行远程调试?
我知道有一些JAVA_OPTS设置可以远程调试Java程序。
这些命令是什么,它们是什么意思?
以下代码对于Java 5及更低版本进行设置。
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044
对于Java 5及更高版本,请使用以下命令运行它:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
输出
java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 HelloWhirled
Listening for transport dt_socket at address: 1044
Hello whirled
提问:如何在Java中为Android设置HttpResponse超时
我创建了以下函数来检查连接状态:
private void checkConnectionStatus() {
HttpClient httpClient = new DefaultHttpClient();
try {
String url = "http://xxx.xxx.xxx.xxx:8000/GaitLink/"
+ strSessionString + "/ConnectionStatus";
Log.d("phobos", "performing get " + url);
HttpGet method = new HttpGet(new URI(url));
HttpResponse response = httpClient.execute(method);
if (response != null) {
String result = getResponse(response.getEntity());
...
当我关闭服务器以测试执行情况时,在下面这等待了很长时间
HttpResponse response = httpClient.execute(method);
有谁知道如何设置超时以避免等待太久?
谢谢!
回答1:
在我的示例代码中,设置了两个超时。连接超时抛出java.net.SocketTimeoutException: Socket is not connected,套接字超时java.net.SocketTimeoutException: The operation timed out。
HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
如果要设置HTTPClient的参数(例如DefaultHttpClient或AndroidHttpClient)
httpClient.setParams(httpParameters);