Uploaded image for project: 'Spring for Android'
  1. Spring for Android
  2. ANDROID-81

IllegalArgumentException: No matching constant for [-1] after upgrading from M4 to RC

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 1.0.0.RC1
    • Fix Version/s: 2.0.0.M1
    • Component/s: rest-template
    • Labels:
      None
    • Environment:
      Honeycomb

      Description

      An issue was reported in the forum that upgrading to RC1 resulted in the IllegalArgumentException. Switching to use the HttpComponentsClientHttpRequestFactory resolves the issue. Prior to RC1, Rest Template defaulted to the HttpComponentsClientHttpRequestFactory, but now in versions greater than Froyo (2.2), it defaults to SimpleClientHttpRequestFactory. It may be that SimpleClientHttpRequestFactory was not working in any of the previous versions either. Here is the reported Rest Template construction being used:

      HttpHeaders requestHeaders = new HttpHeaders();
      List<MediaType> acceptableMediaTypesXml = new ArrayList<MediaType>();
      acceptableMediaTypesXml.add(MediaType.APPLICATION_XML);
      requestHeaders.set("WsUsername", "username");
      requestHeaders.set("WsPassword", "password");
      requestHeaders.setAcceptEncoding(ContentCodingType.GZIP);
      if (ticket != null)

      { requestHeaders.set("Ticket", ticket); }

      requestHeaders.set("TerminalID", UnoMobilApplication.getTerminalId());
      restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory()); // since default doesn't work...
      requestHeaders.setAccept(acceptableMediaTypesXml);
      HttpEntity<?> requestEntity = new HttpEntity<String>(id, requestHeaders);
      ResponseEntity<DeliveryList> responseEntity = restTemplate.exchange(url + "deliveries/" + id, HttpMethod.GET, requestEntity, DeliveryList.class);
      return responseEntity.getBody();

        Activity

        Hide
        rclarkson Roy Clarkson added a comment -

        Need more information

        Show
        rclarkson Roy Clarkson added a comment - Need more information
        Hide
        mweisser Michael Weisser added a comment -

        Hi,

        i have the same problem and you can reproduce it with the following steps:

        I use the latest "Spring for Android 1.0.1 Release" and use the "Android Emulator" with an "API-Level 7 AVD" BUT this also happens on a real device...

        1) Create and resuse a "RestTemplate" with a "SimpleClientHttpRequestFactory"
        2) Use the following HttpHeaders and RestTemplate calls:

        HttpHeaders lRequestHeaders = new HttpHeaders();
        lRequestHeaders.setAcceptEncoding(ContentCodingType.GZIP);
        lRequestHeaders.set("Connection", "Close");
        lRequestHeaders.set("Cache-Control", "no-cache");
        lRequestHeaders.set("Pragma", "no-cache");

        HttpEntity<?> lRequestEntity = new HttpEntity<Object>(lRequestHeaders);
        ResponseEntity<byte[]> lResponseEntity = mRestTemplate.exchange(lUrl, HttpMethod.GET, lRequestEntity, byte[].class);

        3) Open the URL "http://dl.dropbox.com/u/4904201/springerror/13-02-19.CSV" using the options under 2). This results in a "404 NOT FOUND" error and this is correct.

        4) Now open the URL "http://dl.dropbox.com/u/4904201/springerror/13-02-18.CSV" using the options under 2). This results in the mentioned "java.lang.IllegalArgumentException: No matching constant for [-1]"!!!

        Thanks and Regards

        Show
        mweisser Michael Weisser added a comment - Hi, i have the same problem and you can reproduce it with the following steps: I use the latest "Spring for Android 1.0.1 Release" and use the "Android Emulator" with an "API-Level 7 AVD" BUT this also happens on a real device... 1) Create and resuse a "RestTemplate" with a "SimpleClientHttpRequestFactory" 2) Use the following HttpHeaders and RestTemplate calls: HttpHeaders lRequestHeaders = new HttpHeaders(); lRequestHeaders.setAcceptEncoding(ContentCodingType.GZIP); lRequestHeaders.set("Connection", "Close"); lRequestHeaders.set("Cache-Control", "no-cache"); lRequestHeaders.set("Pragma", "no-cache"); HttpEntity<?> lRequestEntity = new HttpEntity<Object>(lRequestHeaders); ResponseEntity<byte[]> lResponseEntity = mRestTemplate.exchange(lUrl, HttpMethod.GET, lRequestEntity, byte[].class); 3) Open the URL "http://dl.dropbox.com/u/4904201/springerror/13-02-19.CSV" using the options under 2). This results in a "404 NOT FOUND" error and this is correct. 4) Now open the URL "http://dl.dropbox.com/u/4904201/springerror/13-02-18.CSV" using the options under 2). This results in the mentioned "java.lang.IllegalArgumentException: No matching constant for [-1] "!!! Thanks and Regards
        Hide
        rclarkson Roy Clarkson added a comment - - edited

        Michael Weisser thanks for the additional information!

        Show
        rclarkson Roy Clarkson added a comment - - edited Michael Weisser thanks for the additional information!
        Hide
        tbruyelle Thomas Bruyelle added a comment -

        FYI, I fixed this issue with this workaround found in a Stackoverflow's answer :

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        }

        Show
        tbruyelle Thomas Bruyelle added a comment - FYI, I fixed this issue with this workaround found in a Stackoverflow's answer : if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { restTemplate.setRequestFactory( new HttpComponentsClientHttpRequestFactory()); }
        Hide
        rclarkson Roy Clarkson added a comment -

        Thanks, with Michael's example, I'm now able to reliably reproduce the problem with HttpUrlConnection. I'm only seeing this specific issue on Android 2.1 and 2.2. I believe I have a fix for it, but I need to work on tests that aren't dependent on hitting github. Thomas, your fix will certainly work, as using the HttpComponents client doesn't suffer from the problems of HttpUrlConnection. I've even considered making the HttpComponents client the default for all versions of Android.

        Show
        rclarkson Roy Clarkson added a comment - Thanks, with Michael's example, I'm now able to reliably reproduce the problem with HttpUrlConnection. I'm only seeing this specific issue on Android 2.1 and 2.2. I believe I have a fix for it, but I need to work on tests that aren't dependent on hitting github. Thomas, your fix will certainly work, as using the HttpComponents client doesn't suffer from the problems of HttpUrlConnection. I've even considered making the HttpComponents client the default for all versions of Android.
        Hide
        tbruyelle Thomas Bruyelle added a comment -

        What's the benefit of HttpUrlConnection over HttpComponents ?

        Show
        tbruyelle Thomas Bruyelle added a comment - What's the benefit of HttpUrlConnection over HttpComponents ?
        Hide
        rclarkson Roy Clarkson added a comment -

        The HttpComponents client on Android is older and not being maintained or improved. In comparison the standard J2SE facilities continue to improve from version to version. Google recommends using HttpURLConnection on Gingerbread and newer versions of Android.

        Show
        rclarkson Roy Clarkson added a comment - The HttpComponents client on Android is older and not being maintained or improved. In comparison the standard J2SE facilities continue to improve from version to version. Google recommends using HttpURLConnection on Gingerbread and newer versions of Android.
        Hide
        rclarkson Roy Clarkson added a comment -

        Resolve issue with reusing connections on Froyo (2.2) and older
         
        In certain circumstances when attempting to reuse a connection on
        Android 2.2 and older, you may receive the following exception:
         
        IllegalArgumentException: No matching constant for [-1]
         
        For those affected versions, the HTTP Keep-Alive header is set to false
        via the following system property:
         
        System.setProperty("http.keepAlive", "false");
         
        Issue: ANDROID-81
        

        Show
        rclarkson Roy Clarkson added a comment - Resolve issue with reusing connections on Froyo (2.2) and older   In certain circumstances when attempting to reuse a connection on Android 2.2 and older, you may receive the following exception:   IllegalArgumentException: No matching constant for [-1]   For those affected versions, the HTTP Keep-Alive header is set to false via the following system property:   System.setProperty("http.keepAlive", "false");   Issue: ANDROID-81

          People

          • Assignee:
            rclarkson Roy Clarkson
            Reporter:
            rclarkson Roy Clarkson
          • Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: