正文  网络通信 > HTTP >

android如何调用REST服务

[代码]rest services 的services方法01publicstaticvoidmain(String[] args)throwsThrowable {02// this can create JAX-RS server objects03JAXRSServerFactoryBean sf =newJAXRSServerFactoryBean......

[代码] rest services 的services方法

01 public static void main(String[] args) throws Throwable {
02         // this can create JAX-RS server objects
03         JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
04         sf.setResourceClasses(BooksResource.class, BookResource.class,
05                 BookSelectionsResource.class);
06         sf.setAddress("http://10.0.0.101:8080/bs");
07         sf.create();
08         System.out.println("Started");
09         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10         for (;;) {
11             System.out.println("Enter command: u--update. q--quit");
12             String cmd = br.readLine();
13             if (cmd.equals("u")) {
14                 BookDB.instance.getBook("1234").setLastModified(new Date());
15             } else if (cmd.equals("q")) {
16                 System.exit(0);
17             }
18         }
19     }
20  
21         XJCFacade.main(new String[] { "-b", "src/main/resources/bindings.xml",
22                 "-d", "src/main/java", "src/main/resources/BookService.xsd" });
23         System.out.println("Done!");
24     }

[代码] android 客户端

01 public void onClick(View view) {
02             try {
03                 TextView tvResult = (TextView) findViewById(R.id.myView);
04                 HttpClient client = new DefaultHttpClient();
05                 HttpGet httpGet = new HttpGet(
06                         "http://10.0.0.101:8080/bs/books/1234");
07                 HttpResponse response = client.execute(httpGet);
08                 InputStream inputStream = response.getEntity().getContent();
09                 StringBuffer buffer = new StringBuffer();
10                 BufferedReader bufferReader = new BufferedReader(
11                         new InputStreamReader(inputStream));
12                 String str = new String("");
13                 while ((str = bufferReader.readLine()) != null) {
14                     buffer.append(str);
15                 }
16                 bufferReader.close();
17                 System.out.println(buffer.toString());
18 //这里得到的是一个json数据类型的              
19 tvResult.setText(buffer.toString());
20                                   //转换就省略了
21             } catch (Throwable e) {
22                 new RuntimeException(e);
23             }
24         }

[代码] [XML]代码

01 <?xml version="1.0" encoding="UTF-8"?>
02 <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ttdev.com/bs"
03     xmlns:tns="http://ttdev.com/bs" elementFormDefault="qualified">
04     <element name="book">
05         <complexType>
06             <sequence>
07                 <element name="isbn" type="string"></element>
08                 <element name="title" type="string"></element>
09             </sequence>
10         </complexType>
11     </element>
12     <element name="books">
13         <complexType>
14             <sequence>
15                 <element ref="tns:book" minOccurs="0" maxOccurs="unbounded"></element>
16             </sequence>
17         </complexType>
18     </element>
19     <element name="reviews">
20         <complexType>
21             <sequence>
22                 <element ref="tns:reviewRef" minOccurs="0" maxOccurs="unbounded"></element>
23             </sequence>
24         </complexType>
25     </element>
26     <element name="reviewRef">
27         <complexType>
28             <sequence>
29                 <element name="summary" type="string"></element>
30                 <element name="url" type="anyURI"></element>
31             </sequence>
32         </complexType>
33     </element>
34     <element name="review">
35         <complexType>
36             <sequence>
37                 <element name="by" type="string"></element>
38                 <element name="text" type="string"></element>
39             </sequence>
40         </complexType>
41     </element>
42 </schema>

[代码] [XML]代码

01 <?xml version="1.0" encoding="UTF-8"?>
02 <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
03     xmlns:xsd="http://www.w3.org/2001/XMLSchema" schemaLocation="BookService.xsd"
04     jaxb:version="2.0">
05     <jaxb:bindings node="/xsd:schema/xsd:element[@name='book']">
06         <jaxb:class name="BookState"></jaxb:class>
07     </jaxb:bindings>
08     <jaxb:bindings node="/xsd:schema/xsd:element[@name='books']">
09         <jaxb:class name="BooksState"></jaxb:class>
10     </jaxb:bindings>
11     <jaxb:bindings node="/xsd:schema/xsd:element[@name='reviews']">
12         <jaxb:class name="ReviewsState"></jaxb:class>
13     </jaxb:bindings>
14     <jaxb:bindings node="/xsd:schema/xsd:element[@name='review']">
15         <jaxb:class name="ReviewState"></jaxb:class>
16     </jaxb:bindings>
17 </jaxb:bindings>
18  
19 <!--上面的2个文件 都是直接从的实验中复制的 呵呵 自行修改吧-->