【问题标题】:Calling an RESTFul using Spring-Android使用 Spring-Android 调用 RESTFul
【发布时间】:2012-10-23 09:01:13
【问题描述】:

这是我的 Strifified json,

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

我正在使用 Gson 构建它。 String Stringifiedjson = new Gson().toJson(user); 这是我想要达到的目标。

RestTemplate rest = new RestTemplate();
String url = "";
String event = rest.getForObject(url, Stringifiedjson);
  1. 我将如何发送到我的 REST 服务并在 onEventHandler 或 onErrorHandler 中取回我的结果。我基本上来自JavaScript 背景。
  2. 为什么getForObject方法不接受String, String作为参数。

更新:

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);

附件是我遇到的异常情况。

http://pastie.org/private/efyfvvbxyxdsvm3lvv7q

【问题讨论】:

    标签: java android spring gson


    【解决方案1】:

    关于第二个问题:我刚刚找到了这个例子(你可以看一下整个文档;))

    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
    

    您的堆栈跟踪中的异常可能与此post 的同一问题有关。当您的应用尝试在主线程中建立连接时,就会出现此问题。

    【讨论】:

    • 我在哪里传递我的 JsonObject .... 让我使用 postForObject... 但我没有看到在这里传递 JsonObject。
    • 我猜你使用的是 org.springframework.web.client,对吗?您能否添加更多有关您的问题的信息?编辑答案以添加部分参考。
    • 我已经更新了我的帖子,我正在使用 Spring-Android。我在这里做错了什么......
    • 通过查看异常...您是否为您的应用授予了正确的权限?你能分享一下你正在使用的skd版本吗?也看看stackoverflow.com/questions/6343166/…
    【解决方案2】:
    10-23 15:46:10.106: E/AndroidRuntime(1038): FATAL EXCEPTION: main
    10-23 15:46:10.106: E/AndroidRuntime(1038): android.os.NetworkOnMainThreadException
    

    当您在应用程序主 ui 线程中执行任何网络操作时,会抛出 NetworkOnMainThreadException(另请参阅 Keeping Your App Responsive。这是不允许的。您必须使用后台线程进行网络操作,请参阅 Worker threads on http://developer.android.com/guide/components/processes-and-threads.html

    【讨论】:

      猜你喜欢
      • 2014-03-22
      • 2012-05-19
      • 2012-06-30
      • 2017-07-08
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多