android連結rest services

來源:互聯網
上載者:User

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個檔案 都是直接從的實驗中複製的 呵呵 自行修改吧

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.