我想知道是否有可能为Android应用程序创建API .jar,以便我能够像这样使用它:
HelloWorldExampleAPI api;
HelloWorld helloWorld = api.getHelloWorldConnector();
helloWorld.displayMessage("How are you?");
而不是像这样实施AsyncTasks
:
class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
String msg = "";
for(String m : uri) { msg += m; }
String url = "http://192.168.42.229:8080/HelloWorldExample/helloWorld/displayMessage/" + msg;
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP GET request, marshaling the response to a String
return restTemplate.getForObject(url, String.class, "Android");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
从这个简单的servlet示例:
@Controller
@RequestMapping("/helloWorld")
public class HelloWorldController {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(ModelMap model) {
System.out.println("/hello");
model.addAttribute("msg", "JCG Hello World!");
return "helloWorld";
}
@RequestMapping(value = "/displayMessage/{msg}", method = RequestMethod.GET)
public String displayMessage(@PathVariable String msg, ModelMap model) {
System.out.println("/displayMessage/{msg} : " + msg);
model.addAttribute("msg", msg);
return "helloWorld";
}
}