REST PUT响应格式

时间:2012-09-03 16:44:47

标签: java android http rest

我有一个带有MySQL数据库的android应用程序。在Android平板电脑上输入的行需要在服务器上同步,也需要与MySql同步。写入服务器后,需要将服务器Unique Integer返回到平板电脑以更新本地数据库。这样它会将客户端x-ref到服务器。它目前的工作原理是为每一行执行PUT并在响应中使用服务器ID的位置。但是,如果存在大量更新,则需要很长时间,因为每一行都会打开一个新的HttpConnection。我希望能够将更新分组到一个XML字符串中,并获取一个XML文件,该文件返回的是每个客户机ID的serverID的xreference。但是我找不到在响应中发送XML的方法,只有URI。我的连接代码是

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, Const.HTTP_CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, Const.HTTP_SOCKET_TIMEOUT);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
client.getCredentialsProvider().setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("xxxxx","xxxxx"));  // TODO Change to logged user name
HttpPut put = new HttpPut(uri);
StringEntity entity = new StringEntity(xml);
entity.setContentType("application/xml");
put.setEntity(entity);
HttpClientParams.setRedirecting(put.getParams(), false);
HttpResponse response = client.execute(put);
switch (response.getStatusLine().getStatusCode()){
case 200:
case 201:
    location = response.getLastHeader("Location").getValue();
    key = location.substring(location.lastIndexOf("/")+1);
    break;
case 401:
    throw new RuntimeException("Authorisation Failed");
default:
    throw new RuntimeException("Failed to Create Data Record on Server");
            }

在服务器上

if (flight.getClientFlightId()!=0){
if (insertFlight(userId)){
    xref += "<clientId>"+flight.getClientFlightId()+"</clientId><serverId>"+flight.getServerFlightId()+"</serverId>";
    }
}
xref +="</xref>";
response = Response.created(URI.create(xref)).build();

有人能帮忙吗?

1 个答案:

答案 0 :(得分:0)

ArrayList<NameValuePair> putParams = new ArrayList<NameValuePair>();
putParams.add(new BasicNameValuePair("myXML", "<xml> _PUT_YOUR_XML_HERE_ </xml>"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(putParams);
put.setEntity(formEntity);