现在在做一个android应用,http是用httpclient接口实现的。
现在Get方法没问题,但是Post方法有点不懂。如发表一条豆瓣说POST http://api.douban.com/miniblog/saying<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban.com/xmlns/">
<content>收拾屋子就是把书从床头搬到书架的过程</content>
</entry>现在是把xml数据转变为StringEntity来发送,但是状态返回为400。想问一下有谁用httpclient的,xml数据应该怎样设置?
现在Get方法没问题,但是Post方法有点不懂。如发表一条豆瓣说POST http://api.douban.com/miniblog/saying<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban.com/xmlns/">
<content>收拾屋子就是把书从床头搬到书架的过程</content>
</entry>现在是把xml数据转变为StringEntity来发送,但是状态返回为400。想问一下有谁用httpclient的,xml数据应该怎样设置?
void testPost() throws IOException, OAuthException {
OAuthConsumer consumer = new DoubanOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
consumer.setTokenWithSecret(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
String url = "http://api.douban.com/miniblog/saying";
HttpPost request = new HttpPost(url);
String say = "<?xml version='1.0' encoding='UTF-8'?>" +
"<entry xmlns:ns0=\"http://www.w3.org/2005/Atom\" xmlns:db=\"http://www.douban.com/xmlns/\">" +
"<content>test from jxphone-douban</content>" +
"</entry>";
StringEntity entity = new StringEntity(say, HTTP.UTF_8);
entity.setContentType("text/xml");
request.setHeader("Content-Tyep", "application/atom+xml");
request.setEntity(entity);
consumer.sign(request);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode() + "");
}