0

残りの Web サービスを使用してサーバーにコンテンツを送信する Android アプリケーションを開発しています。

単純なパラメーター (strings、int、...) を使用するとうまく機能しますが、いくつかのオブジェクトを送信したいことを知っており、POST 請願を通じてオブジェクトの XML 形式をサーバーに送信しようとしています。しかし、415 コード (「サポートされていないメディアの種類」) を受け取りましたが、何が原因なのかわかりません。Firefox の POSTER プラグインを使用すると投稿データを Web サービスに送信でき、正常に応答するため、xml が問題ないことはわかっていますが、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);

ヒントはありますか?何が起こっているのか本当にわかりません。xml を送信するので、ヘッダー http に何かを指定する必要があるのでしょうか? 覚えておいてください: 単純なデータでは問題なく動作します。

4

2 に答える 2

0

コンテンツタイプをに設定する必要があります

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

詳細については、このを参照してください。

于 2012-06-23T17:17:34.693 に答える
0

を使用してxmlをサーバーに投稿するには、この方法を試してください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"));
}
于 2012-06-23T17:24:39.960 に答える