0

カスタム Linux dist を使用し、RESTful API を使用して、組み込みデバイスで実行される Java プログラムを作成しています。インターネット接続がダウンしても、このプログラムを継続してほしいです。

それを確実にするために、指定された接続タイムアウトを設定しましたが、ランダムに動作するようです。

これが私のコードです:

HttpURLConnection connection = null;
String query = String.format("key=%s&deviceId=%d", key, deviceId);
String response = "";

try
{
    URL requestUrl = new URL(REST_API_URL + api + "/?" + query);
    System.out.println("URL: " + requestUrl.toString());
    if ((connection = (HttpURLConnection) requestUrl.openConnection()) == null)
    {
        Log.severe("Cannot open HTTP connection.", null);
    }

    // Set HTTP options
    connection.setRequestMethod("GET");
    connection.setRequestProperty("Accept", "text/json");
    connection.setRequestProperty("Content-Type", "text/json");
    connection.setRequestProperty("Accept-Charset", CHARSET);
    connection.setConnectTimeout(2000);
    connection.setReadTimeout(2000);

    connection.connect();

    // Check if the response was OK
    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
    {
        Log.severe("HTTP connection failed : " + connection.getResponseMessage(), null);
    }

    // Get the response stream
    InputStream input = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    response = reader.readLine();
}
catch (IOException e)
{
    Log.severe(e.getMessage(), e);
}
finally
{
    if (connection != null)
    {
        connection.disconnect();
    }
}

問題は、インターネットを切断すると、しばらくして (~20 秒) 許容できない java.net.UnknownHostException がスローされることです。実際には、タイムアウトは無視されます。

[ここ][1] 一部の Android デバイスでは、HttpURLConnection クラスにバグがある可能性があると読みました。そのため、最悪の org.apache.http.client.HttpClient も試しました (醜い構文で半分非推奨のパッケージをインストールする必要がありました)。問題は解決しません。

最新のクラスを含むコードは次のとおりです。

String query = String.format("key=%s&deviceId=%d", key, deviceId);
String response = "";

try
{
    // Set HTTP parameters
    URI requestUri = new URI(REST_API_URL + api + "/?" + query);
    System.out.println("URL: " + requestUri.toString());

    RequestConfig requestConfig = RequestConfig.custom()
       .setSocketTimeout(2000)
       .setConnectTimeout(2000)
       .setConnectionRequestTimeout(2000)
       .build();

    HttpGet httpGet = new HttpGet(requestUri);
    httpGet.setConfig(requestConfig);
    httpGet.addHeader("Accept", "text/json");
    httpGet.addHeader("Content-Type", "text/json");

    // Create a client
    CloseableHttpClient httpClient = HttpClients.createDefault();

    // Run the HTTP request
    HttpResponse httpResponse = httpClient.execute(httpGet);

    // Check the response code
    if (httpResponse.getStatusLine().getStatusCode() != 200)
    {
        Log.severe("HTTP connection failed : " + httpResponse.getStatusLine().getReasonPhrase(), null);
    }

    // Get the response stream
    InputStreamReader input = new InputStreamReader(httpResponse.getEntity().getContent());
    BufferedReader reader = new BufferedReader(input);
    response = reader.readLine();

    // Release the connection
    httpClient.close();
}
catch (IOException | URISyntaxException e)
{
    Log.severe(e.getMessage(), e);
}

何か案が ?

編集

/etc/resolv.conf ファイルに DNS ルックアップのタイムアウトを設定するオプションを追加しましたが、それも無視されているようです。

ここに私の resolv.conf ファイルがあります:

search einet.ad.eivd.ch # eth0
nameserver 10.192.22.5 # eth0
nameserver 10.192.57.10 # eth0
options timeout:1
4

0 に答える 0