0

I have a problem in my android Background service. While send the data using http protocol it shows ClientProtocolException

My WebAPI is

 [AcceptVerbs("GET", "POST")]
 [ActionName("InsertLocationDetails")]
 public string InsertLocationDetails([FromBody]LocationDetails locDetails )
 {
 return objLocationService.InsertLocationDetails(locDetails.UserId,locDetails.DateTime.ToString(),
 locDetails.Latitude,locDetails.Longitude,locDetails.Status.ToString(),locDetails.Location.ToString());
 }

I have my LocationDetails class with paramaters

 public class LocationDetails
 {
 public int UserId { get; set; }
 public Double Latitude { get; set; }
 public Double Longitude { get; set; }
 public string Status { get; set; }
 public string DateTime { get; set; }
 public string Location { get; set; }
 }

In my Android background code i m using http protocol. I need to communicate with my web api service to insert the datas

 public String insertLocationDetails(int userid,String newtime,String status,double latitude,double longitude,String address,String city,String country) 
{ 
 HostUrl="http://URL/ServiceName/InsertLocationDetails"; 
 HttpClient httpClient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost(HostUrl);
 try
 { 
 List<NameValuePair> params = new LinkedList<NameValuePair>();
 params.add(new BasicNameValuePair("UserId",String.valueOf(userid)));
 params.add(new BasicNameValuePair("DateTime", newtime));
 params.add(new BasicNameValuePair("Latitude",String.valueOf(latitude)));
 params.add(new BasicNameValuePair("Longitude",String.valueOf(longitude)));
 params.add(new BasicNameValuePair("Status",status));
 params.add(new BasicNameValuePair("Location",(address+","+city+","+country)));
 httpPost.setHeader("Content-Type", "application/json");
 httpPost.setHeader("Accept", "application/json");
 HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
 httpPost.setEntity(entity);
 ResponseHandler<String> handler = new BasicResponseHandler();
 result =httpClient.execute(httpPost,handler);
 }
 catch (ClientProtocolException e)
 { 
 e.printStackTrace();
 Log.e(TAG, "ClientProtocolException in callWebService(). " + e.getMessage());
 }
 catch (IOException e)
 { 
 e.printStackTrace();
 Log.e(TAG, "IOException in callWebService(). " + e.getMessage());
 }
 return result;
 }}

Actually my jquery code is working properly and inserting values

My Javascript code is working properly

function insertLocationDetails()
{
 var url=serverUrl();
 var urlpath={
 UserId : userid,
 DateTime : datetime, 
 Latitude: latitude,
 Longitude : longitude, 
 Status: status,
 Location : address };
 $.ajax
 ({
 cache: false,
 async: true,
 type: "POST",
 dataType: "json",
 contentType: "application/json;charset=utf-8",
 url: url +"InsertLocationDetails",
 data: JSON.stringify(urlpath),
 success: function (result) 
 {
 var result = eval(result);
 for (var property in result) 
 {
 }
 }
 });
}

Kindly help me figure out the Exception in my Android Background service please give a correct code to make communication with my service

Thanks in advance ARUN

asked Jun 28, 2013 at 13:50
1
  • hei @Arun how did you fix your problem here I tried your code and I get the same error the ClientProtocolException became null what is wrong can you please help me how would your fix it thanks Commented Aug 1, 2013 at 2:28

1 Answer 1

1

You are sending the request body as application/x-www-form-urlencoded but setting content type as JSON.

Instead of using;

`httpPost.setHeader("Content-Type", "application/json");`

use;

`httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");`
BlackPearl
2,9045 gold badges40 silver badges54 bronze badges
answered Jun 28, 2013 at 14:55

1 Comment

I have Changed the code but still its giving Clientprotocol Exception and internal sever error. Can u suggest me where i should correct my code..In android or in my Webapi service

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.