4

I've run into a bit of a deadend and need a bit of help (please)!

I'm very new to Android Dev (and to coding in general). Basically I need to POST XML data to a URL using HttpURLConnection but can't get it to work. I've got my app reading and pasrsing XML data from a GET request but finding the POST part difficult.

I've looked at creating a NameValuePair array but not sure how to do this with the XML structure I am needing to post.

The XML data will look like this:

<Sheet>
  <Job>jobNumber</Job> 
  <Task>taskNumber</Task> 
  <UserID>3</UserID> 
  <Date>systemDateFormatted</Date> 
  <Minutes>timeToLog</Minutes> 
  <Note>userNote</Note>
</Sheet>

So far I have this for my code.

try {
        URL url = new URL(theUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("Sheet", null));
        params.add(new BasicNameValuePair("Job", jobNumber));
        params.add(new BasicNameValuePair("Task", taskNumber));
        params.add(new BasicNameValuePair("UserID", String.valueOf(yourUserID)));
        params.add(new BasicNameValuePair("Date", systemDateFormatted));
        params.add(new BasicNameValuePair("Minutes", timeElapsed));
        params.add(new BasicNameValuePair("UserNote", "Test Note"));
        params.add(new BasicNameValuePair("Sheet", null));

I'm not sure if I'm understanding NamedValuePair right. Would it be better to create a string for my XML data and POST this way instead?

Thanks!

Metalor
  • 627
  • 2
  • 6
  • 5

2 Answers2

2

Yes, POST data goes as payload of your request. For example

URL url = new URL(theUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);
    conn.setDoOutput(true);
    String body = "<xml...</xml>";
    OutputStream output = new BufferedOutputStream(conn.getOutputStream());
    output.write(body.getBytes());
    output.flush();
finally {
    conn.disconnect();
}
Volodymyr Lykhonis
  • 2,936
  • 2
  • 17
  • 13
  • AWESOME!! Thanks heaps! I managed to get it posting correctly with this (after I figured out I was using the wrong Extras varible name from the previous activity, haha). – Metalor May 15 '15 at 04:18
1

I'm not sure if I'm understanding NamedValuePair right. Would it be better to create a string for my XML data and POST this way instead?

Your post seems to be cut off, but from what you show what you're doing is not posting XML but adding query parameters.

Convert your XML to an encoded string, then write it to the output stream you get from conn.getOutputStream().

Here's a similar example: https://stackoverflow.com/a/2737455/1197251

You would replace "query" with your XML string.

Community
  • 1
  • 1
dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • Thanks very much mate! I created a string for the XML data I needed to post and once I figured out where the null value was coming from it is working nicely. Cheers! – Metalor May 15 '15 at 04:20