Submit XML for POST request in Android

In my code, I allow users to fill out a form, and I save this form in an XML file on the SD card.

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

How can i do this.

I have already searched the Internet and found a sample code, but I do not know how to add an 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 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();
    }
}
+3
source share
1 answer

Your entity should be XML, not a pair of values ​​that is intended for headers:

 request.setEntity(new StringEntity(postData));

This is the snippet 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
 }
+2
source

Source: https://habr.com/ru/post/1783628/


All Articles