开发者

Send XML for POST-request in Android

In my code I let users fill in a form and I save that form in a XML file on the SD-card.

Now I need to send the information on the XML-file as POSTdata to a server.

How can I do that.

I already searched the internet and found some sample-code, but I don't know how to add the XML file located on my SD-card.

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

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); //<-- need t开发者_运维百科o 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();
    }
}


Your entity must be the XML and not the value pair which is meant for the headers:

 request.setEntity(new StringEntity(postData));

This is a snippet that I have:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜