2012-06-23 65 views
0

我正在开发一个Android应用程序,该应用程序使用其他Web服务将内容发送到服务器。发送邮件xml到REST

使用简单参数(字符串,int,...)它很好,但知道我想发送一些对象,我试图通过POST将对象的XML形式发送到服务器请愿。但是我收到一个415代码(“不支持的媒体类型”),我不知道可能是什么。我知道xml是可以的,因为使用Firefox的POSTER插件,您可以将发布数据发送到Web服务,并且响应正常,但通过Android我无法做到。

这里是代码我使用:

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); 

任何提示?我真的不知道发生了什么事。也许我需要在标头http中指定任何内容,因为我发送了一个xml文件?记住:使用简单的数据,它工作正常。

回答

0

您需要的内容类型设置为

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

请参阅本example了解更多详情。

+0

这是行不通的 – Frion3L

+1

我已经固定发布的XML服务器。这是内容类型 - > application/xml – Frion3L

0

尝试这种方式使用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")); 
} 
+0

这完全改变了我的web服务客户端... – Frion3L