android链接rest services

rest services 的services方法

public static void main(String[] args) throws Throwable {
        // this can create JAX-RS server objects
        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setResourceClasses(BooksResource.class, BookResource.class,
                BookSelectionsResource.class);
        sf.setAddress("http://10.0.0.101:8080/bs");
        sf.create();商账追收
        System.out.println("Started");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        for (;;) {
            System.out.println("Enter command: u--update. q--quit");
            String cmd = br.readLine();
            if (cmd.equals("u")) {
                BookDB.instance.getBook("1234").setLastModified(new Date());
            } else if (cmd.equals("q")) {
                System.exit(0);
            }
        }
    }

        XJCFacade.main(new String[] { "-b", "src/main/resources/bindings.xml",
                "-d", "src/main/java", "src/main/resources/BookService.xsd" });
        System.out.println("Done!");
    }

自己修改相应的字段

android 客户端

public void onClick(View view) {
            try {
                TextView tvResult = (TextView) findViewById(R.id.myView);
                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(
                        "http://10.0.0.101:8080/bs/books/1234");
                HttpResponse response = client.execute(httpGet);
                InputStream inputStream = response.getEntity().getContent();
                StringBuffer buffer = new StringBuffer();
                BufferedReader bufferReader = new BufferedReader(
                        new InputStreamReader(inputStream));
                String str = new String("");
                while ((str = bufferReader.readLine()) != null) {
                    buffer.append(str);
                }
                bufferReader.close();
                System.out.println(buffer.toString());
//这里得到的是一个json数据类型的               
tvResult.setText(buffer.toString());
                                  //转换就省略了
            } catch (Throwable e) {
                new RuntimeException(e);
            }
        }

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ttdev.com/bs"
    xmlns:tns="http://ttdev.com/bs" elementFormDefault="qualified">
    <element name="book">
        <complexType>
            <sequence>
                <element name="isbn" type="string"></element>
                <element name="title" type="string"></element>
            </sequence>
        </complexType>
    </element>
    <element name="books">
        <complexType>
            <sequence>
                <element ref="tns:book" minOccurs="0" maxOccurs="unbounded"></element>
            </sequence>
        </complexType>
    </element>
    <element name="reviews">
        <complexType>
            <sequence>
                <element ref="tns:reviewRef" minOccurs="0" maxOccurs="unbounded"></element>
            </sequence>
        </complexType>
    </element>51赛尔号
    <element name="reviewRef">
        <complexType>
            <sequence>
                <element name="summary" type="string"></element>
                <element name="url" type="anyURI"></element>
            </sequence>
        </complexType>
    </element>
    <element name="review">
        <complexType>
            <sequence>
                <element name="by" type="string"></element>
                <element name="text" type="string"></element>
            </sequence>
        </complexType>
    </element>
</schema>

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" schemaLocation="BookService.xsd"
    jaxb:version="2.0">
    <jaxb:bindings node="/xsd:schema/xsd:element[@name='book']">
        <jaxb:class name="BookState"></jaxb:class>
    </jaxb:bindings>
    <jaxb:bindings node="/xsd:schema/xsd:element[@name='books']">
        <jaxb:class name="BooksState"></jaxb:class>
    </jaxb:bindings>
    <jaxb:bindings node="/xsd:schema/xsd:element[@name='reviews']">
        <jaxb:class name="ReviewsState"></jaxb:class>
    </jaxb:bindings>
    <jaxb:bindings node="/xsd:schema/xsd:element[@name='review']">
        <jaxb:class name="ReviewState"></jaxb:class>
    </jaxb:bindings>
</jaxb:bindings>

上面的2个文件 都是直接从的实验中复制的 呵呵 自行修改吧

posted @ 2011-05-26 09:48  sky7034  阅读(296)  评论(0编辑  收藏  举报
蘑菇街女装 货运专家