function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
indyindy 

Apex rest from Java : Unsupported Media Type exception

Hi,

 

If I execute the below java code, The Http Response in debug log is:

 

Http Response****HTTP/1.1 415 Unsupported Media Type [Date: Tue, 03 Sep 2013 09:36:54 GMT, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked]

 

Can somebody please look into that and suggest the solution.

 

Java client application:
===============================================================================================================================
private static void updateAccount(String instanceUrl,String accToken) throws HttpException, UnsupportedEncodingException
{

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("pdnNo", "14785"));
HttpPost post1 = new HttpPost(instanceUrl+"/services/data/v28.0/sobjects/Account/");
post1.setHeader("Authorization", "OAuth " + accToken);
post1.setHeader("content-type","text/html");
post1.setEntity(new UrlEncodedFormEntity(urlParameters,HTTP.UTF_8));

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("pdnNo", "14785"));

try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(post1);
System.out.println("Http Response****"+httpResponse);
Map<String,String> updResponse = (Map<String,String>)JSONValue.parse(EntityUtils.toString(httpResponse.getEntity()));
for(Map.Entry<String, String> entry: updResponse.entrySet())
{
System.out.println(String.format("%s = %s",entry.getKey(),entry.getValue()));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}


Saleforce apex Rest:
======================================================================================================================
@RestResource(urlMapping='/Account/*')
global class AF_IB_Integration
{
@HttpGet
global static Account getAccount()
{
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String accountId = req.requestURI.subString(req.requestURI.lastIndexOf('/')+1);
Account accRes = [Select Id,Name,Phone,WebSite FROM Account where id = :accountId limit 1];
return accRes;
}
@HttpPost
global static void updateAccount(String pdnNo)
{
if(pdnNo != null)
{
Account accRes = [Select Id,Primary_Dealer_Number__c,Type from Account Where Primary_Dealer_Number__c = :pdnNo];
accRes.Type= 'Integration';
update accRes;
}
}
}