2012-09-03 76 views
0

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

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

任何人能帮助请吗?

回答

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); 
+0

不是我的意思。在PUT中获取xml可以正常工作。我只想要一个XML。 – PeterB