0

I'm developing an Android application which sends content to a server using rest web services.

With simple parameters (strings, int, ...) it works great, but know I would like to send some objects and I'm trying to do it sending the XML form of the object to the server through a POST petition. But I'm receiving a 415 code ("Unsupported Media Type"), and I don't know what could be. I know the xml is OK because with the POSTER plugin of firefox you can send post data to the web service and it responds ok, but through the Android I am not able to do it.

Here is the code I'm using:

ArrayList<NameValuePair>() params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("customer", "<customer>   <name>Bill Adama</name>     <address>lasdfasfasf</address></customer>");

HttpPost request = new HttpPost(url);  
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

HttpClient client = new DefaultHttpClient(); 
HttpResponse httpResponse = client.execute(request);

Any hint? I dont really know what is going on. Maybe do I need to specify anything in the header http because I send an xml? Remember: with simple data it works fine.

Frion3L
  • 1,502
  • 4
  • 24
  • 34

2 Answers2

0

You need to set content-type to

conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");

See this example for more details.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
0

try this way for posting xml to server using DefaultHttpClient()

String strxml= "<customer><name>Bill Adama</name><address>lasdfasfasf</address></customer>";
InputStream is = stringToInputStream(strxml);  
HttpClient client = new DefaultHttpClient();  
HttpPost post = new HttpPost(PATH);  
InputStreamBody isb = new InputStreamBody(is, "customer.xml");  
MultipartEntity multipartEntity = new MultipartEntity();  
multipartEntity.addPart("file", isb);  
multipartEntity.addPart("desc", new StringBody("this is description."));  
post.setEntity(multipartEntity);  
HttpResponse response = client.execute(post);  
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
    is = response.getEntity().getContent();  
     String result = inStream2String(is);   
  }  

public InputStream stringToInputStream(String text) throws UnsupportedEncodingException {
    return new ByteArrayInputStream(text.getBytes("UTF-8"));
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213