2012-09-03 98 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