2011-01-05 13 views
3

私のコードでは、ユーザーがフォームに記入して、そのフォームをSDカードのXMLファイルに保存します。AndroidでPOSTリクエストのXMLを送信

XMLファイルの情報をPOSTデータとしてサーバーに送信する必要があります。

どうすればいいですか?

すでにインターネットを検索してサンプルコードを見つけましたが、SDカードにあるXMLファイルを追加する方法がわかりません。

public void sendtoRoutemobiel() { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://mysite/"); 

    try { 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); //<-- need to replace this with 
     nameValuePairs.add(new BasicNameValuePair("id", "12345"));   //<-- the data on the XML-file 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));   //<-- (I think) 

     HttpResponse response; 
     response = httpclient.execute(httppost); 
     String temp1 = EntityUtils.toString(response.getEntity()); 
     Log.d("Gabug", "Response: " + temp1); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

答えて

2

あなたの実体はXMLではなく、ヘッダーのために意味される値のペアでなければなりません。

public static String getStringContent(String uri, String postData, 
     HashMap<String, String> headers) throws Exception { 

     HttpClient client = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(); 
     request.setURI(new URI(uri)); 
     request.setEntity(new StringEntity(postData)); // HERE !!!!!! _______ 
     for(Entry<String, String> s : headers.entrySet()) 
     { 
      request.setHeader(s.getKey(), s.getValue()); 
     } 
     HttpResponse response = client.execute(request); 
     // .. rest 
} 
+0

をだから私はどこかの操作を行います。これは私が持っているスニペットがある

request.setEntity(new StringEntity(postData)); 

私のXMLファイルの場所を宣言しますか? ( 'Environment.getExternalStorageDirectory()+" /Message.xml "') – Galip

+0

私はこの 'httppost.setEntity(新しいStringEntity(Environment.getExternalStorageDirectory()+" /MessageRM.xml "));)が好きでした。ありがとう! – Galip

+0

バックエンドのデータをどのようにfecthするのですか? – onkar

関連する問題