{"id":2157,"date":"2022-08-30T15:22:31","date_gmt":"2022-08-30T15:22:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/strange-problems-with-the-spring-resttemplate-in-android-application-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:31","modified_gmt":"2022-08-30T15:22:31","slug":"strange-problems-with-the-spring-resttemplate-in-android-application-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/strange-problems-with-the-spring-resttemplate-in-android-application-collection-of-common-programming-errors\/","title":{"rendered":"Strange problems with the Spring RestTemplate in Android application-Collection of common programming errors"},"content":{"rendered":"<p>Nice question. Once had a similar problem. I think you targeting your app Android 4.0-4.2. Then you must perform all your operations in another thread. You perform authorization as I&#8217;ve seen from your code &#8211; it&#8217;s a short operation, so it&#8217;s better for you to use AsyncTask for this instead of util.concurrent pacakge. Here is howto for this on androidDevelopers:<\/p>\n<p>http:\/\/developer.android.com\/reference\/android\/os\/AsyncTask.html<\/p>\n<p>You should override doInBackground(Params&#8230;) in such a way:<\/p>\n<pre><code>class LoginTask extends AsyncTask\n{\n    @Override\n    protected Void doInBackground(String... params) {\n        HttpHeaders requestHeaders = new HttpHeaders();\n        requestHeaders.setContentType(MediaType.APPLICATION_JSON); \n        HttpEntity _entity = new HttpEntity(requestHeaders);\n        RestTemplate templ = new RestTemplate();\n        templ.setRequestFactory(new HttpComponentsClientHttpRequestFactory());\n        templ.getMessageConverters().add(new MappingJacksonHttpMessageConverter());\n        ResponseEntity _response = templ.postForEntity(params[0],_entity,null) \/\/null here in order there wasn't http converter errors because response type String and [text\/html] for JSON are not compatible;\n        String _body =  _response.getBody();\n        return null;\n    }\n}\n<\/code><\/pre>\n<p>And then call it in your BeginAuthorization(View view):<\/p>\n<pre><code>new LoginTask().execute(_URL);\n<\/code><\/pre>\n<p>Hope this helps.<\/p>\n<p id=\"rop\"><small>Originally posted 2014-01-04 02:48:46. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Nice question. Once had a similar problem. I think you targeting your app Android 4.0-4.2. Then you must perform all your operations in another thread. You perform authorization as I&#8217;ve seen from your code &#8211; it&#8217;s a short operation, so it&#8217;s better for you to use AsyncTask for this instead of util.concurrent pacakge. Here is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2157","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2157"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2157\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}