main code:- class UpdateListener implements OnClickListener {
- @Override
- public void onClick(View view) {
- try {
- TextView tvResult = (TextView) findViewById(R.id.myView);
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(
- "http://10.0.0.101:8080/bs/books");
- JSONObject object = new JSONObject();
- object.put("<SPAN style="COLOR: #ff0000">ns1.isbn</SPAN>", "12345");
- object.put("<SPAN style="COLOR: #ff0000">ns1.title</SPAN>", "C#");
- JSONObject book = new JSONObject();
- book.put("<SPAN style="COLOR: #ff0000">ns1.book</SPAN>", object);
- StringEntity str = new StringEntity(book.toString());
- // str.setContentType("application/json; charset=UTF-8");
- // str.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
- // "application/json; charset=UTF-8"));
- httpPost.setEntity(str);
- httpPost.addHeader("Accept", "application/json");
- httpPost.addHeader("Content-type", "application/json");
- HttpResponse response = httpClient.execute(httpPost);
- tvResult.setText("State:"
- + response.getStatusLine().getStatusCode());
- } catch (Throwable e) {
- new RuntimeException(e);
- }
- }
- }
- class UpdateListener implements OnClickListener {
- @Override
- public void onClick(View view) {
- try {
- TextView tvResult = (TextView) findViewById(R.id.myView);
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(
- "http://10.0.0.101:8080/bs/books");
- JSONObject object = new JSONObject();
- object.put("ns1.isbn", "12345");
- object.put("ns1.title", "C#");
- JSONObject book = new JSONObject();
- book.put("ns1.book", object);
- StringEntity str = new StringEntity(book.toString());
- // str.setContentType("application/json; charset=UTF-8");
- // str.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
- // "application/json; charset=UTF-8"));
- httpPost.setEntity(str);
- httpPost.addHeader("Accept", "application/json");
- httpPost.addHeader("Content-type", "application/json");
- HttpResponse response = httpClient.execute(httpPost);
- tvResult.setText("State:"
- + response.getStatusLine().getStatusCode());
- } catch (Throwable e) {
- new RuntimeException(e);
- }
- }
- }//红色的哪个ns1是默认的命名空间 我迟点再想想有没有更好的办法.直接可以把ns1.book变为book 呵呵
复制代码 |