Http Put Request
Solution 1:
I recently had to figure out a way to get my android app to communicate with a WCF service and update a particular record. At first this was really giving me a hard time figuring it out, mainly due to me not knowing enough about HTTP
protocols, but I was able to create a PUT
by using the following:
URLurl=newURL("http://(...your service...).svc/(...your table name...)(...ID of record trying to update...)");
//--This code works for updating a record from the feed--HttpPuthttpPut=newHttpPut(url.toString());
JSONStringerjson=newJSONStringer()
.object()
.key("your tables column name...").value("...updated value...")
.endObject();
StringEntityentity=newStringEntity(json.toString());
entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
entity.setContentEncoding(newBasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
httpPut.setEntity(entity);
// Send request to WCF service DefaultHttpClienthttpClient=newDefaultHttpClient();
HttpResponseresponse= httpClient.execute(httpPut);
HttpEntityentity1= response.getEntity();
if(entity1 != null&&(response.getStatusLine().getStatusCode()==201||response.getStatusLine().getStatusCode()==200))
{
//--just so that you can view the response, this is optional--intsc= response.getStatusLine().getStatusCode();
Stringsl= response.getStatusLine().getReasonPhrase();
}
else
{
intsc= response.getStatusLine().getStatusCode();
Stringsl= response.getStatusLine().getReasonPhrase();
}
With this being said there is an easier option by using a library that will generate the update methods for you to allow for you to update a record without having to manually write the code like I did above. The 2 libraries that seem to be common are odata4j
and restlet
. Although I haven't been able to find a clear easy tutorial for odata4j
there is one for restlet
that is really nice: http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx?CommentPosted=true#commentmessage
Post a Comment for "Http Put Request"