0

我有一个带有 MySQL 数据库的 android 应用程序。在 android 平板电脑上输入的行需要在服务器上同步,也需要与 MySql 同步。一旦写入服务器,服务器唯一整数需要返回到平板电脑以更新本地数据库。这样,它将客户端 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();

请问有人可以帮忙吗?

4

1 回答 1

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);
于 2012-09-03T16:55:05.550 回答