use of com.tomasio.projects.trainning.jackson.Category in project trainning by fernandotomasio.
the class TesteMoodle4HTTP method main.
public static void main(String[] args) throws UnsupportedEncodingException, MalformedURLException, ProtocolException, IOException {
String token = ConfigHelper.getValue("moodle.token");
String domainName = ConfigHelper.getValue("moodle.domain");
// Also possible in Moodle 2.2 and later: 'json'
String restformat = "jsoSn";
// Setting it to 'json' will fail all calls on earlier Moodle version
if (restformat.equals("json")) {
restformat = "&moodlewsrestformat=" + restformat;
} else {
restformat = "";
}
String postFunctionName = "core_course_create_categories";
String serverurl = domainName + "/webservice/rest/server.php" + "?wstoken=" + token + "&wsfunction=" + postFunctionName + restformat;
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
list.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(list);
// String urlParameters
// = "categories[0][name]=" + URLEncoder.encode("testusername1", "UTF-8")
// + "&categories[0][parent]=" + URLEncoder.encode("1", "UTF-8")
// + "&categories[0][idnumber]=" + URLEncoder.encode("", "UTF-8")
// + "&categories[0][description]=" + URLEncoder.encode("", "UTF-8")
// + "&categories[0][descriptionformat]=" + URLEncoder.encode("1", "UTF-8");
//
//
// HttpURLConnection con = (HttpURLConnection) new URL(serverurl).openConnection();
// con.setRequestMethod("POST");
// con.setDoOutput(true);
// con.setUseCaches(false);
// con.setDoInput(true);
// DataOutputStream wr = new DataOutputStream(
// con.getOutputStream());
// wr.writeBytes(urlParameters);
// wr.flush();
// wr.close();
//
// InputStream is = con.getInputStream();
// BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// String line;
// StringBuilder response = new StringBuilder();
// while ((line = rd.readLine()) != null) {
// response.append(line);
// response.append('\r');
// }
// rd.close();
// System.out.println(response.toString());
Category category = new Category();
category.setName("teste8");
category.setParent(1);
category.setDescriptionformat(1);
Category[] categories = new Category[1];
categories[0] = category;
restTemplate.postForLocation(serverurl, categories);
// System.out.println(courses.length);
//
// MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
// map.add("fullname", "sadfasdfasdf");
// map.add("shortname", "asdfasdfasdfasdf");
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
//
// HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
//
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
// messageConverters.add(new MappingJackson2HttpMessageConverter());
// messageConverters.add(new FormHttpMessageConverter());
// restTemplate.setMessageConverters(messageConverters);
// Course response = restTemplate.postForObject(serverurl, request, Course.class);
//
// System.out.println(response.getFullname());
// Course c = new Course();
// c.setFullname("alskjdfçasdf");
// c.setShortname("ldslkdf");
// c.setCategoryid(1);
// List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
// list.add(new MappingJackson2HttpMessageConverter());
// restTemplate.setMessageConverters(list);
// Course course = restTemplate.postForObject(serverurl, c, Course.class);
// System.out.println(course.getFullname());
}
Aggregations