日期:2014-05-16  浏览次数:20337 次

android链接rest services并解析json(附带实例 更新实例2011.5.31)

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);
			}
		}
	}

?public static void main(String[] args) throws Throwable {

		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>
	<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>
	&l