Announcement Announcement Module
Collapse
No announcement yet.
IllegalArgumentException: No matching constant for [-1] after upgrading from M4 to RC Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    I just upgraded from Spring for Android 1.0.0 M4 to RC1 and now I am gettting the following exception when trying to call my rest service the second time (first call works ok..):

    Code:
    java.lang.IllegalArgumentException: No matching constant for [-1]
    03-08 18:23:09.204 D/UnoMobil(  584): 	at org.springframework.http.HttpStatus.valueOf(HttpStatus.java:380)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at org.springframework.http.client.SimpleClientHttpResponse.getStatusCode(SimpleClientHttpResponse.java:49)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:46)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:457)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:419)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:395)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at no.asp.as.unomobil.services.rest.DeliveryService.getDeliveries(DeliveryService.java:38)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at no.asp.as.unomobil.tasks.DeliveryListTask.doInBackground(DeliveryListTask.java:56)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at no.asp.as.unomobil.tasks.DeliveryListTask.doInBackground(DeliveryListTask.java:23)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at android.os.AsyncTask$2.call(AsyncTask.java:252)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
    03-08 18:23:09.204 D/UnoMobil(  584): 	at java.lang.Thread.run(Thread.java:1020)

    What am I missing?


    Regards,

    BTJ

  • #2
    Ok, if I use HttpComponentsClientHttpRequestFactory instead of the default SimpleClientHttpRequestFactory then it works.... Does this mean the default should not be used or?

    BTJ

    Comment


    • #3
      Hi, on which version of Android are you testing or running? It sounds like you reusing the same RestTemplate object when you make a "second request", is that correct? Any more details would be appreciated. As a workaround you can certainly use the HttpComponents client. Thanks!

      Comment


      • #4
        I was testing under Android 3.2...
        Also I thought about reusing the RestTemplate and now I became a bit unsure... I was certain I didn't reuse the RestTemplate but now I have to check again...

        Btw, what are the pros and cons of using the HttpComponent vs SimpleClient?


        BTJ

        Comment


        • #5
          You should be able to reuse a RestTemplate object to make multiple requests. Google recommends using the standard J2SE facilities in Gingerbread and newer because of functionality improvements and bug fixes. In the RC1 release, Spring for Android now defaults to this in Gingerbread and newer. Here is a some additional information from Google:

          http://android-developers.blogspot.c...p-clients.html

          Comment


          • #6
            So, any explanation to why I get IllegalArgumentException?

            BTJ

            Comment


            • #7
              It looks like the response is empty from the second request, and the default http status of -1 is not valid, which is the IllegalArgumentException. HttpStatus.valueOf() is expecting a valid http status, such as 200, for example. This can happen if there is an error when making the request, which is what looks like happened in the stack trace you provided.

              I would like to investigate your scenario. Any additional details would be helpful. If I understand correctly, you are reusing a RestTemplate instance to make multiple requests on Android 3.2. The HttpComponents client works, but the Simple client does not. I'll start with that.

              Comment


              • #8
                I don't think that I am reusing RestTemplate but I will double check at work tomorrow... But the rest is correct...
                And yes, the error seems to occur before the request gets to the server...

                Any more info you would like?

                BTJ

                Comment


                • #9
                  No, I don't reuse my RestTemplate....

                  BTJ

                  Comment


                  • #10
                    Ok, thanks. Can you provide the setup you are using when creating your request with RestTemplate? I don't need any proprietary information, but it would be helpful to know how you are configuring your request.

                    Comment


                    • #11
                      I am creating the request like this..:

                      Code:
                              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();

                      BTJ

                      Comment


                      • #12
                        Thanks for the information on how you are constructing the Rest Template request. I've created a new issue to track this [1], so it can be addressed in the next release.

                        [1] https://jira.springsource.org/browse/ANDROID-81

                        Comment


                        • #13
                          Can you try the latest build snapshot? I've made some changes in SimpleHttpClient to fix an issue with basic auth, and I'm curious if you are still experiencing this. What is the HTTP status code returned in the response?

                          Comment


                          • #14
                            Hello there.

                            I am getting this error now as well.. I have been using the 1.0.0 RELEASE and now with the spring-android-rest-template-1.0.1.BUILD-20121023.140702-8.

                            Well, if I start from the top, I am using the rest template to get content from two different servers.. one server using regular http, and the other using https..

                            I noticed that on some devices (even newer devices as Samsung Galaxy S3), it seems as though making multiple calls through HTTPS is very slow.. ie. making 2-3 calls in a row would result in a ~3 second delay between each call (but not on all devices). So I went to google and found a post saying that using the HttpComponentsClientHttp over HTTPS could result in poor performance. Also, I was reading that from gingerbread and up i should use the SimpleClientHttp.. Or just leave it out entirely to let the framework decide based on android version.

                            So, what happens now is that using either SimpleClient or "nothing" I get this error "IllegalArgumentException: No matching constant for [-1]", on my device that i did not have before. I believe it happens right before executing the second HTTPS call. The first one seems to come through just fine. However, it seems there are no issues with regular http calls (at least I have two HTTP calls before the two HTTPS calls).

                            Also notice it is different on different devices:

                            Using HttpComponentsClientHttp
                            Galaxy Nexus (Android 4.1.2) -> No performance Issues, works great
                            Galaxy S3 (Android 4.1.1) -> Performance issue (~3 second delay between calls) but works.

                            Using SimpleClientHttp
                            Galaxy Nexus -> Fails with IllegalArgumentException in second Https call.
                            Galaxy S3 -> not tested.

                            Code:
                            Code:
                                           // is working but has performance issues on some devices
                                           // RestTemplate template = new RestTemplate(true, new HttpComponentsClientHttpRequestFactory());
                            
                                           RestTemplate template = new RestTemplate(true);
                            
                            		HttpHeaders headers = getHeaders()
                            		
                            		if(cookies != null) {
                            			addCookieHeaders(headers, cookies);
                            		}
                            		
                            		HttpEntity<?> entity = null;
                            		
                            		if(json != null && json.length() > 0) {
                            			entity = new HttpEntity<Object>(json, headers);
                            		}
                            		else {
                            			entity = new HttpEntity<Object>(headers);
                            		}
                            		
                            		ResponseEntity<String> result = null;
                            		
                            		try {
                            			// Fire!
                            			result = template.exchange(
                            				url,
                            				method, 
                            				entity, 
                            				String.class);
                            		}
                            		catch(HttpClientErrorException ex) {
                            		}
                            		catch(HttpServerErrorException ex) {
                            		}
                            		catch(Exception ex) {
                            		}
                            		
                            		return result;
                            Any help would be appreciated..
                            Thank you very much.
                            David
                            Last edited by davidj.dk; Nov 5th, 2012, 05:40 AM.

                            Comment


                            • #15
                              I've been trying to reproduce this exception with no luck so far. I'm using a modified version of the twitter search sample app. I changed the url to use https instead, and I added several repeated restTemplate.exchange() calls. I've tested both http clients on a Galaxy Nexus (4.1.1), Nexus One (2.3.6), and Nexus 7 (4.2.1) without seeing an exception. Do you have any additional information that can help reproduce this issue?

                              Comment

                              Working...
                              X