2010-12-28 10 views
0

を送る=/アンドロイド:私はプログラミングに新しいですし、私はそれにいくつかの助けを必要とするドキュメントオブジェクトとしてXML、POSTメソッド

Webサービスは、すでに私が書いていないが、してください。私がしなければならないことは、Webサービスを通じてPOSTメソッドでXMLをドキュメントオブジェクトとして送信することだけです。

私のコード:

public class send extends application { 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://app.local/test/"); 

     try { 
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 
      Document document = documentBuilder.newDocument(); 
      Element rootElement = document.createElement("packet"); 
      rootElement.setAttribute("version", "1.2"); 
      document.appendChild(rootElement); 

      Element em = document.createElement("imei"); 
      em.appendChild(document.createTextNode("000000000000000")); 
      rootElement.appendChild(em); 

      em = document.createElement("username"); 
      em.appendChild(document.createTextNode("5555")); 
      rootElement.appendChild(em); 

      HttpResponse response = httpclient.execute(httppost); 

     } catch (Exception e) { 
      System.out.println("XML Pasing Excpetion = " + e); 
     } 
    } 

}

+0

私はhttp://developer.androidを見ていることでしょう、あなたの問題

を解決することがあります希望.com/reference/org/apache/http/HttpMessage.html#addHeader%28java.lang.String、%20java.lang.String%29。どのようにこれを投稿する必要がありますか?アプリケーション/ x-www-form-urlencodedまたはmultipart/form-data? – kellogs

+0

コンテンツタイプはtext/xmlでなければなりません。この値を送ると、私はログインします。 –

答えて

1

私は、Androidのプログラミングにも非常に新しいです。しかし、私は以下の方法で発行したものを解決しました。

public void send(){ 

    DefaultHttpClient httpClient = new DefaultHttpClient(); 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("Servername", "abc")); 
    nameValuePairs.add(new BasicNameValuePair("UserName", "123)); 
    nameValuePairs.add(new BasicNameValuePair("PassWord", "123")); 
    nameValuePairs.add(new BasicNameValuePair("XML", getRequestTypeStringBuilder())); 

    // Your every parameter name must be match with passing parameter, otherwise it throw 
    // an exception if it in case sensitive 

    HttpPost httpPost = new HttpPost("http://app.local/DeviceLogin/"); 
    httpPost.addHeader("Accept", "text/xml"); 
    httpPost.setHeader("Content-Type","application/xml;charset=UTF-8"); 
    httpPost.setEntity(nameValuePairs); 

    HttpResponse response = httpClient.execute(httpPost);  

    // Be aware, if your return data type is also xml, then using replace empty string, 
    // otherwise, it my not retrieve or seen all data. 

} 

private static String getRequestTypeStringBuilder(){ 
    StringBuilder body = new StringBuilder("<?xml version=\"1.0 \"encoding=\"UTF-8\"?>"); 
    body.append("<!DOCTYPE My System\"Abc.dtd\">"); 
    // Please append detail your xml body in here; 
    return body.toString(); 
} 

これは "すべてのビーイングは幸せである"

よろしくとメッタ、 Ichirohangリンブ

+0

この行のnameValuePairs.add(新しいBasicNameValuePair( "UserName"、 "123)); 123の後に" –

関連する問題