0

I am using the HttpPut to communicate with server in Android, the response code I am getting is 500.After talking with the server guy he said prepare the string like below and send.

{"key":"value","key":"value"}

now I am completely confused that where should i add this string in my request.

Please help me out .

Om Narain Shukla
  • 361
  • 1
  • 4
  • 12
  • For security reason you should avoid to use Put and Delete methods. PUT can be dangerous if it is not properly locked down. – jsaye May 03 '12 at 14:08

2 Answers2

8

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:

    URL url = new URL("http://(...your service...).svc/(...your table name...)(...ID of record trying to update...)");

    //--This code works for updating a record from the feed--
    HttpPut httpPut = new HttpPut(url.toString());
    JSONStringer json = new JSONStringer()
    .object() 
       .key("your tables column name...").value("...updated value...")
    .endObject();

    StringEntity entity = new StringEntity(json.toString());
    entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
    httpPut.setEntity(entity); 

    // Send request to WCF service 
    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpResponse response = httpClient.execute(httpPut);                     
    HttpEntity entity1 = response.getEntity(); 

    if(entity1 != null&&(response.getStatusLine().getStatusCode()==201||response.getStatusLine().getStatusCode()==200))
    {
         //--just so that you can view the response, this is optional--
         int sc = response.getStatusLine().getStatusCode();
         String sl = response.getStatusLine().getReasonPhrase();
    }
    else
    {
         int sc = response.getStatusLine().getStatusCode();
         String sl = 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

cking24343
  • 3,173
  • 1
  • 21
  • 23
  • Your code worked for me, thanks. I'm sure thought this code can be optimized? – Eugene van der Merwe Sep 25 '12 at 13:57
  • @cking24343 my app is sending data via HttpPut method.when server got request, it reply as json data. I don't know how get data from json. please tell me. [CODE](http://pastie.org/7443759) – kongkea Apr 11 '13 at 03:06
  • @kongkea there are several ways to parse through the json data to get what you want out of it. Here is an [example](http://stackoverflow.com/questions/2255220/how-to-parse-a-json-and-turn-its-values-into-an-array) – cking24343 Apr 15 '13 at 16:06
  • Sorry, I didn't see your code sample you provided. [Here](http://pastie.org/7600870) is something that you could try based on what I saw from your example, tested it locally on my machine and its working. Let me know if you have any other issues. – cking24343 Apr 16 '13 at 15:54
1

Error 500 is Internal Server error. Not sure if this answers your question but I personally encountered it when trying to send a data URI for an animated gif in a PUT request formatted in JSON but the data URI was too long. You may be sending too much information at once.

cmarangu
  • 204
  • 4
  • 16