This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.

Question: What Java command line options can be set to allow remote debugging of the JVM?

I know that there are some JAVA_OPTS Settings that allow you to debug Java programs remotely.

What are these commands? What do they mean?

The following code sets up Java 5 and lower.

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044
Copy the code

For Java 5 and later, run it using the following command:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044
Copy the code

The output

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 HelloWhirled
Listening for transport dt_socket at address: 1044
Hello whirled
Copy the code

Q: How do I set HttpResponse timeout for Android in Java

I created the following function to check the connection status:

private void checkConnectionStatus(a) {
    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()); .Copy the code

When I shut down the server to test execution, THERE was a long wait down here

HttpResponse response = httpClient.execute(method);
Copy the code

Does anyone know how to set a timeout to avoid waiting too long?

Thank you very much!

Answer 1:

In my sample code, I set two timeouts. Connection timeout thrown java.net.SocketTimeoutException: Socket is not connected, the Socket java.net.SocketTimeoutException: overtime 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);
Copy the code

If you want to set the HTTPClient parameters (such as DefaultHttpClient or AndroidHttpClient)

httpClient.setParams(httpParameters);
Copy the code