Answered by:
Call azure mobile service update function from android using Http

Question
-
Hi,
i am trying to call azure mobile service update function from android app using Http call and it's keep failed.
does anyone know what is the problem
HttpClient httpСlient = getHttpClient();
HttpResponse response;
String responseString = null;
HttpPut httpRequest = new HttpPut(url);
httpRequest.addHeader("X-ZUMO-APPLICATION", applicationKey);
httpRequest.addHeader("X-ZUMO-ACCEPT", "application/json");
httpRequest.addHeader("ACCEPT", "application/json");
httpRequest.setEntity(new UrlEncodedFormEntity(nameValuePair));
ResponseObject respObj = new ResponseObject();
response = httpСlient.execute(httpRequest);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
respObj.setResponseStatus(ResponseStatus.OK);
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
try {
responseString = out.toString();
} catch (Exception e) {
}
} else if (statusLine.getStatusCode() == HttpStatus.SC_CONFLICT) {
respObj.setResponseStatus(ResponseStatus.Conflicts);
responseString = null;
}
else {
respObj.setResponseStatus(ResponseStatus.IinternalServerError);
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}Wednesday, August 20, 2014 5:39 PM
Answers
-
There are a few problems with your code:
- Updates in the mobile service are typically implemented with the PATCH HTTP method, not PUT (that's the case in the node.js backend; in the .NET backend you can have a PUT method but you need to implement it yourself).
- You're sending the data as a UrlEncodedFormEntity; that will send the data as a &-separated name-value pair list. You should send the request using JSON (which is supported on the node.js backend), or enable additional formatters for the .NET backend.
- You need to send the Content-Type header if you're sending a content in your request. Also X-ZUMO-ACCEPT is not used in the mobile service, so it will be ignored (maybe that's where you should send the Content-Type header instead)
Another alternative is to use the Android SDK for Azure Mobile Services, which will make it easier to call the mobile service.
Carlos Figueira
- Proposed as answer by CarlosFigueiraMicrosoft employee Thursday, August 21, 2014 5:40 PM
- Marked as answer by CarlosFigueiraMicrosoft employee Thursday, September 18, 2014 4:29 PM
Thursday, August 21, 2014 5:40 PM
All replies
-
Hi,
For information about Android Client Library you could refer the following link:
Also the link below gives the Server Script for the Update Function.
http://msdn.microsoft.com/en-us/library/azure/jj554214.aspx
Regards,
Malar.Thursday, August 21, 2014 10:14 AM -
There are a few problems with your code:
- Updates in the mobile service are typically implemented with the PATCH HTTP method, not PUT (that's the case in the node.js backend; in the .NET backend you can have a PUT method but you need to implement it yourself).
- You're sending the data as a UrlEncodedFormEntity; that will send the data as a &-separated name-value pair list. You should send the request using JSON (which is supported on the node.js backend), or enable additional formatters for the .NET backend.
- You need to send the Content-Type header if you're sending a content in your request. Also X-ZUMO-ACCEPT is not used in the mobile service, so it will be ignored (maybe that's where you should send the Content-Type header instead)
Another alternative is to use the Android SDK for Azure Mobile Services, which will make it easier to call the mobile service.
Carlos Figueira
- Proposed as answer by CarlosFigueiraMicrosoft employee Thursday, August 21, 2014 5:40 PM
- Marked as answer by CarlosFigueiraMicrosoft employee Thursday, September 18, 2014 4:29 PM
Thursday, August 21, 2014 5:40 PM