0

Here is my Strifified json,

 {
       "Request":{
              "Object1":{
                 "Key1":"Value1"
              },
              "Object2":{
                  "Key2":"Value2"
              }
       },
       "Object3":{
                 "Key3":"Value3"
       }
}

I am forming this using Gson. String Stringifiedjson = new Gson().toJson(user); Here is what i am trying to achive.

RestTemplate rest = new RestTemplate();
String url = "";
String event = rest.getForObject(url, Stringifiedjson);
  1. How would i send to my REST Service and get back my result in onEventHandler or onErrorHandler. I am basically from JavaScript background.
  2. Why does the method getForObject does not accept String, String as params.

Update:

AuthenticateUser user = new AuthenticateUser(credential, Header);       
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(new MediaType("application","json"));         
//HttpEntity<AuthenticateUser> requestEntity = new HttpEntity<AuthenticateUser>(user, requestHeaders);


RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
String url = "url";             
String result = restTemplate.postForObject(url, AuthenticateUser.class, String.class);

Attached is the pastie of what exception i am getting.

http://pastie.org/private/efyfvvbxyxdsvm3lvv7q

theJava
  • 14,620
  • 45
  • 131
  • 172

2 Answers2

0

About the second question: I just found this example (you could take a look at the entire doc ;) )

2.7.1 Basic Usage Example The following example shows a query to google for the search term "SpringSource".

String url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={query}";

// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();

// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

// Make the HTTP GET request, marshaling the response to a String
String result = restTemplate.getForObject(url, String.class, "SpringSource");
getForObject

public <T> T getForObject(URI url,
                    Class<T> responseType)
          throws RestClientException
Description copied from interface: RestOperations
Retrieve a representation by doing a GET on the URL . The response (if any) is converted and returned.
Specified by:
getForObject in interface RestOperations
Parameters:
url - the URL
responseType - the type of the return value
Returns:
the converted object
Throws:
RestClientException

The exception in your stacktrace could be related to the same issue of this post. The problem occurs when your app tries to make a connection in the main thread.

Community
  • 1
  • 1
sataniccrow
  • 372
  • 2
  • 7
  • Where am i passing my JsonObject here.... lets take i am using postForObject... but i don't see passing JsonObject here. – theJava Oct 23 '12 at 09:44
  • I guessed you were using org.springframework.web.client, is that right? Can you please add some more info on what your problem is? EDIT the answer to add part of the ref. – sataniccrow Oct 23 '12 at 10:01
  • I have updated my post, i am using Spring-Android. What is the mistake i am doing here... – theJava Oct 23 '12 at 10:15
  • By looking at the exception... Have you given the right permission to your app? Can you share also the skd's version you are using please? take a look at http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception also – sataniccrow Oct 23 '12 at 12:12
0
10-23 15:46:10.106: E/AndroidRuntime(1038): FATAL EXCEPTION: main
10-23 15:46:10.106: E/AndroidRuntime(1038): android.os.NetworkOnMainThreadException

The NetworkOnMainThreadException is thrown when you execute any network operation in your application main ui thread (see also Keeping Your App Responsive. This is not allowed. You'll have to use a background thread for network operation, see Worker threads on http://developer.android.com/guide/components/processes-and-threads.html

lippertsjan
  • 309
  • 1
  • 18